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