sql 查詢記載數成果集某個區間內記載。本站提示廣大學習愛好者:(sql 查詢記載數成果集某個區間內記載)文章只能為提供參考,不一定能成為您想要的結果。以下是sql 查詢記載數成果集某個區間內記載正文
以查詢前20到30條為例,主鍵名為id
辦法一: 先正查,再反查
select top 10 * from (select top 30 * from tablename order by id asc) A order by id desc
辦法二: 應用left join
select top 10 A.* from tablename A
left outer join (select top 20 * from tablename order by id asc) B
on A.id = B.id
where B.id is null
order by A.id asc
辦法三: 應用not exists
select top 10 * from tablename A
where id not exists
(select top 20 * from tablename B on A.id = B.id)
辦法四: 應用not in
select top 10 * from tablename
where id not in
(select top 20 id from tablename order by id asc)
order by id asc
辦法五: 應用rank()
select id from
(select rank() over(order by id asc) rk, id from tablename) T
where rk between 20 and 30
中第五種辦法看上去似乎沒有成績,查了下文檔,當over()用於rank/row_number時,整型列不克不及描寫一個列,所以會發生非預期的後果. 待斟酌下,有甚麼方法可以修正為想要的成果.