描述:實現分頁的一種算法
大體過程:訪客訪問不一樣的分頁,爲這個當前頁生成動態的查詢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