SQL Server实现查询第m条到第n条的数据

时间:2022-01-02 04:46:11
 1 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
 2 Select TOP n-m+1 * 
 3 FROM Table 
 4 Where (id NOT IN (Select TOP m-1 id FROM Table ))  
 5 
 6 
 7 --从TABLE表中取出第m到n条记录 (Exists版本)
 8 Select TOP n-m+1 * FROM TABLE AS a Where Not Exists
 9 (Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id ) 
10 order by id
11 
12 
13 --m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名
14 Select Top n-m+1 * From Table 
15 Where Id>(Select Max(Id) From 
16 (Select Top m-1 Id From Table order By Id Asc) Temp) 
17 order By Id Asc