【发布时间】:2018-01-25 15:26:50
【问题描述】:
我正在寻找具有 last_id 和 order 条件的 MYSQL 分页解决方案,要使用 LIMIT、OFFSET 我需要找出 last_id 的偏移量。
比如我有一个表news(id,title,updated_at),第一页返回3,1,2,5,4,第二页我有3个参数(last_id是4,order by updated_at desc , 限制为 5),我需要通过 updated_at desc 计算出 id 4 订单的偏移量。
+------+-------+---------------------+
| id | title | updated_at |
+------+-------+---------------------+
| 3 | test3 | 2018-01-22 11:30:08 |
+------+-------+---------------------+
| 1 | test1 | 2018-01-21 12:30:08 |
+------+-------+---------------------+
| 2 | test2 | 2018-01-20 12:30:08 |
+------+-------+---------------------+
| 5 | test5 | 2018-01-19 13:30:08 |
+------+-------+---------------------+
| 4 | test4 | 2018-01-18 14:30:08 |
+------+-------+---------------------+
....
+------+-------+---------------------+
| 11 | test11 | 2018-01-17 14:30:08 |
+------+-------+---------------------+
| 12 | test12 | 2018-01-16 15:30:08 |
+------+-------+---------------------+
第二页我要返回的是11,12,....,有什么有效的方法可以实现吗?
【问题讨论】:
-
样本数据和期望的结果将真正阐明您想要做什么。
-
是的。
LIMIT和ORDER -
@e_i_pi,和那个有点不同,可能我的问题应该是如何为给定的id和订单条件找到row_number()?
-
无论
ORDER BY语句如何,您仍将只使用LIMIT 10 OFFSET n语句,其中n = (#pages - 1) * results_per_page