文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>sql的几种分页方法

sql的几种分页方法

时间:2011-04-06  来源:YLWS

分页数:pagesize

页数:pageindex

举例:搜索第3页的前10条记录(21—30的记录)

1.top+not in

select top pagesize * from table where id not in(select top pagesize*(pageindex-1) id from table order by id) order by id
select top 10 * from table where id not in( select top 20 id from table order by id)order by id

2.top + max

select top pagesize * from table where id >(select max(id) from (select top pagesize*(pageindex-1) id from table order by id) as t) order by id
select top 10 * from table where id >( select max(id) from (select top 20 id from table order by id)as t)order by id

3.颠倒top原理:查询table中从m到n的记录,先查出前n条记录,然后将查询出来的记录按倒序排序,再取前n-m条记录,最后再将查出来的记录再按倒序排序,就是最终结果。 

select * from (select top pagesize * from( select top pagesize*pageindex * from table order by id) as t1 order by id desc) as t2 order by id
select * from (select top 10 * from( select top 30 * from table order by id) as t1 order by id desc) as t2 order by id

总结:方案1由于有not效率比较低,方案2用的比较多,效率比较高,方案3当m很大的时候,效率低。

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载