SQL Server查询第31到40条数据


大致分为两种情况:ID连续和ID不连续。

1.ID连续的情况:

select * from A where ID between 31 and 40

2.ID不连续的情况:

(1).两次对表查询,效率较低。

select top 10 * from A where ID not in (select top 30 ID from A)
(2).外层查询没有对表A进行查询,效率提高。
select top 10 * from (select top 40 ID from A order by ID) as a order by a.ID desc 
(3).ROW_NUMBER()函数效率更高,SQL2005以上版本可用。
1 select * from(select *,ROW_NUMBER() over(order by ID)as 'userID' from A) as a where a.userID between 31 and 40


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM