日期:2014-05-18  浏览次数:20642 次

鉴别一条分页语句
SQL code
select top 4 * from userinfo where userid not in (select top 4 userid from userinfo order by userid desc)order by userid desc 
 
  每页大小为4,第二页的信息
 应该显示 id为 5,6,7,8的 可怎么就没要我的效果?

------解决方案--------------------
SQL code
--用一句SQL取出第 m 条到第 n 条记录的方法      
1 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
select * from Item
select top n-m+1 * from TABLE where (id NOT IN (select top m-1 id FROM TABLE ))

2--从TABLE表中取出第m到n条记录 (Exists版本)

SELECT TOP n-m+1 * FROM TABLE AS a WHERE Not Exists 
(Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id ) 
Order by id

3--m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名
这是最好用的一条!
Select Top n-m+1 * From Table Where Id>(Select Max(Id) From 
(Select Top m-1 Id From Table Order By Id Asc) Temp) Order By Id Asc

------解决方案--------------------
探讨
SQL codeselecttop4*from userinfowhere useridnotin (selecttop4 useridfrom userinfoorderby useriddesc)orderby useriddesc
          每页大小为4,第二页的信息
应该显示 id为 5,6,7,8的 可怎么就没要我的效果?

------解决方案--------------------
SQL code
select top 4 * 
from userinfo 
where userid 
in (select top 8 userid from userinfo order by userid desc)order by userid ASC