SQL Server查询第31到40条数据

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

1.ID连续的情况:

select * from A where ID between 31 and 40
View Code

2.ID不连续的情况:

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

select top 10 * from A where ID not in (select top 30 ID from A)
View Code

(2).外层查询没有对表A进行查询,效率提高。

select top 10 * from (select top 40 ID from A order by ID) as a order by a.ID desc 
View Code

(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
View Code

以上是自己做的笔记,学习过程中,好记性不如烂笔头,自己也记录下来,作为成长的一部分。

posted @ 2016-11-03 10:03  Aquaris  阅读(568)  评论(0编辑  收藏  举报