关于SQL分页在不同版本中的操作方法

Oracle中的分页:

select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=startrow

select a1.* from (select student.*,rownum rn from student) a1 where rn between startpage and endpage;(使用较多)

DB2中的分页:

Select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and endrow

MySQL中的分页:

select * from table WHERE … LIMIT 10; #返回前10行
select * from table WHERE … LIMIT 0,10; #返回前10行
select * from table WHERE … LIMIT 10,20; #返回第10-20行数据

SqlServer中的分页有多种:

常用的是用到了row_number()函数,但是只支持SqlServer2005及以上版本

select top pagenum * from (select row_number()over(order by id)rownumber,* from a)a1 where rownumber>startpage

select * from (select row_number()over(order by id)rownumber,* from a) a1 where rownumber>startpage and rownumber<endpage+1

select * from (select row_number()over(order by id)rownumber,* from a) a1 where rownumber between startpage+1 and endpage

select top pagenum * from a where not exists (select 1 from (select top 30 id from a order by id)a1 where a1.id=a.id) order by id

SqlServer2012及以上版本支持这种

select * from 表 order by id OFFSET PageIndex*pagenum ROWS FETCH next pagenum rows only

这种方法是不是很简单,但是这个只有在SQL Server 2012及以上版本中才能使用,无论是从逻辑读取数还是响应时间、实际执行行数等关键参数看,SQL Server 2012提供的OFFSET/FETCH NEXT分页方式都比Row_Number()方式有了较大的提升。

注意:使用该方法必须使用order by ,不然会有语法错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清清竹林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值