描述:实现分页的一种算法
大致过程:访客访问不同的分页,为这个当前页生成动态的查询SQL,然后送到数据库中执行
输入:总条数,每页多少条,第几页,查询的SQL,排序的字段
注意:传入的排序字段需要构成唯一记录,这就意味着不能单独的使用SortOrder这个单独排序
字段反转:SortOrder asc,PostID desc;反转之后是SortOrder desc,PostID asc;
实现过程
访问第一页
访问后半数分页
100条数据 每页显示4条 按照ID降序 排序
当取第一页时:直接对所有数据降序取出前4条
当取第二页时:
1.先按降序获得前8条数据,然后升序排列着8条数据,
2.这样第二页的数据直接跑到了前面,取出4条,
3.然后在倒叙回去就是第二页的数据
同样的以此往下类推
当后半数分页时:
当取最后一页时:将所有数据升序过来,取出前4条即可
观察生成的过程
访问前半数分页
2016-06-17 08:48:34 SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:48:43 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 8 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:44 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 12 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:45 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 16 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:45 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 20 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:46 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 24 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:46 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 28 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:47 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 32 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:47 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 36 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:48:48 SELECT * FROM ( SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID desc LIMIT 40 ) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
访问后半数分页
2016-06-17 08:50:51 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 4 ) AS T ORDER BY PostID desc
2016-06-17 08:51:02 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 5 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:03 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 9 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:03 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 13 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:04 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 17 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:06 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 21 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:07 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 25 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:07 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 29 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:08 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 33 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:09 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 37 ) AS T ORDER BY PostID desc LIMIT 4
2016-06-17 08:51:09 SELECT * FROM ( SELECT * FROM (SELECT * FROM Post) AS T ORDER BY PostID ASC LIMIT 41 ) AS T ORDER BY PostID desc LIMIT 4
- [转]双TOP二分法生成分页SQL类(支持MSSQL、ACCESS)
本文转自:http://www.cnblogs.com/jitian/archive/2011/03/22/1991961.html 博客开张,先发以前的几个老物件儿,以前写下来的,现在发上来权当记录 ...
- 双有序队列算法——处理哈夫曼K叉树的高效算法
算法介绍: 哈夫曼树的思路及实现众所周知,大部分是用堆来维护和实现,这种思路比较清晰,在K比较小的时候处理较快(具体例子接下来再说),而且编程复杂度不是很高,利于应用.但是,其所用的数据结构是树,是在 ...
- 分页查询的两种方法(双top 双order 和 row_number() over ())
--跳过10条取2条 也叫分页select top 2 * from studentwhere studentno not in (select top 2 studentno from studen ...
- MySQL、SqlServer、Oracle三大主流数据库分页查询 (MySQL分页不能用top,因为不支持)
一. MySQL 数据库 分页查询MySQL数据库实现分页比较简单,提供了 LIMIT函数.一般只需要直接写到sql语句后面就行了.LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它 ...
- [BS-28] iOS中分页的几种算法
iOS中分页的几种算法 总记录数:totalRecord 每页最大记录数:maxResult 算法一: totalPage = totalRecord % maxResult == 0 ? total ...
- SQL TOP分页
SQL TOP分页 2010-11-12 16:35:29| 分类: SQL | 标签: |字号大中小 订阅 1.分页方案一:(利用Not In和SELECT TOP分页) 语句形式: ...
- Top Coder算法题目浏览器
作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/top-code-offline-browser/ 关于 左耳朵耗子 ...
- 程序员编程艺术:第三章续、Top K算法问题的实现
程序员编程艺术:第三章续.Top K算法问题的实现 作者:July,zhouzhenren,yansha. 致谢:微软100题实现组,狂想曲创作组. 时间:2011年05月08日 ...
- 【jQuery 分页】jQuery分页功能的实现
自写的jQuery实现分页功能的分页组件: 功能效果如下: 分页组件就是上图中的三部分, 分别放在表格上部 和下部 . 其中, 1>>>页面的代码如下: product.jsp 其 ...
随机推荐
- JAVA经典算法40题(供面试所用)
[程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数 ...
- ubuntu文件夹建立软链接方法
1:预备知识 -s 是代号(symbolic)的意思. 这里有两点要注意:第一,ln命令会保持每一处链接文件的同步性,也就是说,不论你改动了哪一处,其它的文件都会发生相同的变化:第二,ln的链接又软链 ...
- Tomcat安装配置
Tomcat安装配置 很久没有通过博客对学习所得进行记录了. 现在将使用Tomcat的一些经验和心得写到这里,作为记录和备忘.如果有朋友看到,也请不吝赐教. 1.首先是Tomcat的获取和安装. 获取 ...
- VB.net的特殊语法(区别于C#.NET)
1:引入命名空间(Imports) Imports System.Exception Imports System.Data.SqlClient Imports System.Security.Cry ...
- 概率dp专场
专题链接 第一题--poj3744 Scout YYF I 链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...
- iBatis框架基本使用
iBatis框架是Java持久层开发框架,说白了就是前人写了一部分代码(针对数据库操作),我们要做的就是再次开发,拿来框架直接使用. 我们自己开发时,dao层的sql语句都是写死在程序中的,如果查询条 ...
- PHP获取当前时间戳,当前时间、及解决时区问题
一.获取当前时间戳 方法1:通过time函数 time(); 方法2:通过$_SERVER中的REQUEST_TIME元素 $_SERVER['REQUEST_TIME']; 方法3:通过strtot ...
- IOS开发之GCD---dispatch_semaphore
信号量是一个整形值并且具有一个初始计数值,并且支持两个操作:信号通知和等待.当一个信号量被信号通知,其计数会被增加.当一个线程在一个信号量上等待时,线程会被阻塞(如果有必要的话),直至计数器大于零, ...
- mysql:SQL语句操作数据库中表和字段的COMMENT值
转载:http://blog.163.com/inflexible_simple/blog/static/167694684201182601221758/ 参考文档不太给力啊,表注释和字段注释的资料 ...
- javascript event(事件对象)详解
javascript event(事件对象)详解 1. 事件对象 1. 事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 什 ...