首页 > 代码库 > SqlServer 第31到第40条记录的各种玩法

SqlServer 第31到第40条记录的各种玩法

--两次对表A查询效率较低
select top 10 *
from A
where ID not in (select top 30 ID from A)


--外层查询没有对表A查询,效率大有提高
select top 10 *
from (select top 40 * from A order by ID) as t 

order by t.ID desc


--ROW_NUMBER()函数效率更高,sqlserver2005以及以上版本中才可以使用
select * 

from (select ROW_NUMBER() over(order by ID) as ‘sequence‘,A.* from A ) as t 

where t.sequence between 31 and 40

 

SELECT * FROM A Order by ID OFFSET 30 ROWS FETCH NEXT 10 ROWS ONLY

SqlServer 第31到第40条记录的各种玩法