鉴别一条分页语句

fly_Apple10 2010-03-04 02:17:09
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的 可怎么就没要我的效果?
...全文
72 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_Apple10 2010-03-05
  • 打赏
  • 举报
回复
各位大侠
现在这问题我已经解决了
fly_Apple10 2010-03-05
  • 打赏
  • 举报
回复
引用 3 楼 ldslove 的回复:
引用楼主 fly_apple10 的回复:SQL codeselecttop4*from userinfowhere useridnotin (selecttop4 useridfrom userinfoorderby useriddesc)orderby useriddesc           每页大小为4,第二页的信息应该显示 id为 5,6,7,8的 可怎么就没要我的效果?
既然你已经有行号了,试试如下

SQL codedeclare@pagesizeint ,@pagenumintset@pagesize=4--输入set@pagenum=2--输入select*from userinfowhere id>@pagesize*(@pagenum-1)and id<=@pagesize*@pagenum

lz 谢谢你
fly_Apple10 2010-03-04
  • 打赏
  • 举报
回复
引用 2 楼 happyflystone 的回复:
select top 4 *
from userinfo
where userid not in (select top 4 userid from userinfo order by userid asc)
order by userid asc


这是一样的效果
只是他的变为了降序
SQL77 2010-03-04
  • 打赏
  • 举报
回复
select top 4 * 
from userinfo
where userid
in (select top 8 userid from userinfo order by userid desc)order by userid ASC
?
东那个升 2010-03-04
  • 打赏
  • 举报
回复
ID列最好加上聚集索引
东那个升 2010-03-04
  • 打赏
  • 举报
回复
引用楼主 fly_apple10 的回复:
SQL codeselecttop4*from userinfowhere useridnotin (selecttop4 useridfrom userinfoorderby useriddesc)orderby useriddesc
          每页大小为4,第二页的信息
应该显示 id为 5,6,7,8的 可怎么就没要我的效果?

既然你已经有行号了,试试如下

declare @pagesize int ,@pagenum int
set @pagesize= 4--输入
set @pagenum= 2--输入

select *
from userinfo
where id>@pagesize*(@pagenum-1)
and id<=@pagesize*@pagenum
-狙击手- 2010-03-04
  • 打赏
  • 举报
回复
select top 4 *
from userinfo
where userid not in (select top 4 userid from userinfo order by userid asc)
order by userid asc
mxc1225 2010-03-04
  • 打赏
  • 举报
回复
--用一句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

参考!

6,129

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧