星空网 > 软件开发 > 数据库

SQL Server 分页方法汇总

PageSize = 30

PageNumber = 201

方法一:(最常用的分页代码, top / not in)

select top 30 UserId from UserInfo where UserId not in (select top 6000 UserId from UserInfo order by UserId) order by UserId

备注: 注意前后的order by 一致

方法二:(not exists, not in 的另一种写法而已)

select top 30 * from UserLog where not exists (select 1 from (select top 6000 LogId from UserLog order by LogId) a where a.LogId = UserLog.LogId) order by LogId

备注:EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False。此处的 select 1 from 也可以是select 2 from,select LogId from, select * from 等等,不影响查询。而且select 1 效率最高,不用查字典表。效率值比较:1 > anycol > *

方法三:(top / max, 局限于使用可比较列排序的时候)

select top 30 * from UserLog where LogId > (select max(LogId) from (select top 6000 LogId from UserLog order by LogId) a ) order by LogId

备注:这里max()函数也可以用于文本列,文本列的比较会根据字母顺序排列,数字 < 字母(无视大小写) < 中文字符

方法四:(row_number() over (order by LogId))

select top 30 * from ( select row_number() over (order by LogId) as rownumber,* from UserLog)awhere rownumber > 6000 order by LogId

select * from (select row_number()over(order by LogId) as rownumber,* from UserLog)awhere rownumber > 6000 and rownumber < 6030 order by LogId

select * from (select row_number()over(order by LogId) as rownumber,* from UserLog)awhere rownumber between 6000 and 6030 order by LogId

 

select *from (  select row_number()over(order by tempColumn)rownumber,*  from (select top 6030 tempColumn=0,* from UserLog where 1=1 order by LogId)a)bwhere rownumber>6000row_number() 的变体,不基于已有字段产生记录序号,先按条件筛选以及排好序,再在结果集上给一常量列用于产生记录序号以上几种方法参考http://www.cnblogs.com/songjianpin/articles/3489050.html

备注:  这里rownumber方法属于排名开窗函数(sum, min, avg等属于聚合开窗函数,ORACLE中叫分析函数,参考文章:SQL SERVER 开窗函数简介 )的一种,搭配over关键字使用。

方法五:(offset /fetch next, Server.aspx' >SQL Server 2012支持)

select * from UserLog Order by LogId offset 6000 rows fetch next 30 rows only

备注: 性能参考文章《SQL Server 2012使用OFFSET/FETCH NEXT分页及性能测试》

 

参考文档:

1、http://blog.csdn.net/qiaqia609/article/details/41445233

2、http://www.cnblogs.com/songjianpin/articles/3489050.html

3、http://database.51cto.com/art/201108/283399.htm

 




原标题:SQL Server 分页方法汇总

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊产品视频的作用是什么?对于提升Listing转化率究竟有何好处?:https://www.ikjzd.com/articles/1586964650503446529
南美多地业务关停,Shopee放弃开辟新市场:https://www.ikjzd.com/articles/1586964876404330498
亚马逊怒砍「无人配送」,欧美「最后一公里」怎么就不行了?:https://www.ikjzd.com/articles/1586983927608516609
想申诉怎么办?亚马逊的行动计划书POA怎么写?看这一篇就够了!:https://www.ikjzd.com/articles/1586992924742782977
盘点亚马逊五大促销类型如何提报?:https://www.ikjzd.com/articles/1586993417007407106
跨境电商万圣节社媒营销:8个方法助你冲出重围!:https://www.ikjzd.com/articles/1587000866082787329
有威海直到崂山风景区车吗 威海到青岛崂山怎么走最快:https://www.vstour.cn/a/404250.html
特点有哪些?债券的定义?:https://www.vstour.cn/a/404251.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流