mysql的分页是基于limit关键字,oracle的分页是基于rownum行号,SQLserver的分页在下面进行研究,是基于SQLServer2012进行的测试。

0.原来的SQL的所有数据

下面的测试假设每页都是取5条数据。

1.第一种-ROW_NUMBER() OVER()方式(over函数必须有)

(1)取第一页数据

  select * from (
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]
  ) as b where RowId between 1 and 5;

结果:

(2)取第二页数据

  select * from (
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]
  ) as b where RowId between 6 and 10;

结果:

总结:  这种方式采用    RowId BETWEEN 当前页数-1*页大小+1  and 页数*页大小   ,而且包含起始值与结束值。

补充:这种方式的通用写法如下:   原来SQL不能带order by ,但是可以带条件。

原来SQL =     select * from [mydb].[dbo].[user] where name like 'name%'    

拼接分页的模板如下:

 select * from (
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from (
原来SQL
) AS A
) as B
where RowId between 1 and 5;

2.第二种-offset start fetch next page rows only

(1)取第一页

select * from [mydb].[dbo].[user]   order by ID offset 0 rows fetch next 5 rows only;

结果:

(2)取第二页

select * from [mydb].[dbo].[user]   order by ID offset 5 rows fetch next 5 rows only;

结果:

 总结:这种方式的起始值与结束值计算方式: offset 页号*页大小 rows fetch next 页大小 rows only  

3.第三种: top 关键字

(1)取第一页

select top 5 * from [mydb].[dbo].[user]
where ID not in (select top 0 ID from [mydb].[dbo].[user]);

结果:

(2)取第二页

select top 5 * from [mydb].[dbo].[user]
where ID not in (select top 5 ID from [mydb].[dbo].[user]);

结果:

  总结:这种方式只用改内层的 top就可以了:  内层的top后面相当于起始值,计算方式为  (页号-1)*页大小。

补充:这种分页方式的通用模板如下:  这个可以加order by和条件

原来SQL = select * from [mydb].[dbo].[user] where name like 'name%'

select top 5 * from (
原来SQL
) AS A where ID not in (select top 5 ID from [mydb].[dbo].[user]);

4.  ROW_NUMBER() + top 相当于上面1和3的结合使用

(1)取第一页

select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>0;

结果:

(2)取第二页

select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>5;

结果:

  总结:这种方式比较通用, 第一个 top 里面的值 相当于 页大小,第二个rowID>起始值,起始值计算方式为  (页号-1)*页大小

补充:这种分页方式的通用模板如下:    这种方式原来的SQL也不用加排序语句

原来SQL = select * from [mydb].[dbo].[user] where name like 'name%'

select top (5) * from (
select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from (
原来SQL
) as A
) as B where B.RowId>5;

注意:文中SQLServer的AS A这些起别名不能省略。

SQLServer常用分页方式的更多相关文章

  1. sqlserver实现分页的几种方式

    sqlserver实现分页的几种方式 第一种:使用org.springframework.data.domain.Page来进行分页 package com.cellstrain.icell.repo ...

  2. sqlserver的四种分页方式

    第一种:ROW_NUMBER() OVER()方式 select * from ( select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId f ...

  3. Oracle、MySql、SQLServer数据分页查询

    看过此博文后Oracle.MySql.SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: 别名.字段 FROM( SELECT row_nu ...

  4. jQuery中ajax的4种常用请求方式

    jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...

  5. iOS代码加密常用加密方式

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  6. DataGridView 中添加CheckBox和常用处理方式 .

    DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...

  7. Entity Framework后台采用分页方式取数据与AspNetPager控件的使用

    本文是一个对AspNetPager控件使用的笔记! 有关AspNetPager控件可以查看杨涛主页.这是一个开放的自定义ASP.NET控件,支持各种自定义的数据分页方式,使用很方便,而且功能也很强大, ...

  8. [转]几种常见SQL分页方式

    创建环境: create table pagetest ( id ,) not null, col01 int null, col02 ) null, col03 datetime null ) -- ...

  9. PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )

    /* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...

随机推荐

  1. python类继承的重写和super

    给已经存在的类添加新的行为,继承是非常好的实现方式.但是如果要改变行为呢?比如在Python继承扩展内置类,我们的contact类只允许一个名字和一个邮箱,但是如果要对某些人增加电话号码呢?这里可以通 ...

  2. nginx配置打印请求响应内容

    #放在http{}里面 log_format kyh ' [$time_local] "$request" $status \n' 'req_header:"$req_h ...

  3. slider插件制作轮播图

    html代码: <div id="banner_tabs" class="flexslider"> <ul class="slide ...

  4. chrome截图全网页

    1.F12 2.ctrl+shift+p 3.输入:capture 4.选择Capture full size screenshot

  5. db mysql / mysql cluster 5.7.19 / my.cnf / max_binlog_cache_size / binlog

    s mysql修改binlog保存的天数 https://blog.csdn.net/Hu_wen/article/details/80582013 查看binlog过期时间,设置的时间为90天,这个 ...

  6. TensorFlow入门学习(让机器/算法帮助我们作出选择)

    catalogue . 个人理解 . 基本使用 . MNIST(multiclass classification)入门 . 深入MNIST . 卷积神经网络:CIFAR- 数据集分类 . 单词的向量 ...

  7. 5、MyBatis-parameterType 入参封装 Map 流程

    以如下入参为例,MyBatis 版本为 3.5.0 public MyUser selectMyUserIdAndAge(Integer id, @Param("user") My ...

  8. uby on rails 用户密码加密

    运行环境: rails 4.2.1                    ruby 2.0.0p481                   mysql(支持多种数据库) 在实际的项目中,需要注意对用户 ...

  9. linux kill 命令【待完善】【转】

    kill 命令用来处理进程, 在linux中即可使用kill -9 pid 杀死进程 , 也可使用kill -KILL pid 等价的命令来执行. HUP 1 终端断线 INT 2 中断(同 Ctrl ...

  10. 算法实践——舞蹈链(Dancing Links)算法求解数独

    在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dancing Links)算法求解精确覆盖问题. 本文介绍该算法的实际运用,利用舞蹈链(Dancin ...