我知道如何访问表中的最后 N 条记录,即
SELECT * FROM table_name ORDER BY auto_incremented_id DESC LIMIT N;
有N条记录意味着表记录每天都在递增,每条记录都有唯一的序列号,从1到N。 现在我想检索最后 N-10 行的 10 条记录。
请看例子
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record N-M
Record N-2
Record N-1
Record N
我如何检索 N-2 到 N-M 行
网站所需的解决方案 - 1st 页面显示最后 10 条记录,2nd 页面显示倒序的最后 10 条记录,但行显示在第一页并且它继续到表的第一个记录。
最佳答案
你可以玩排序和你的限制条件
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT N) as temp ORDER BY auto_incremented_id ASC LIMIT M
假设你说的分页大小为 10
第一页:
SELECT * from as temp ORDER BY auto_incremented_id DESC LIMIT 10;
第二页:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 20) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
第三页:
SELECT * from (SELECT * from table_name ORDER BY auto_incremented_id DESC LIMIT 30) as temp ORDER BY auto_incremented_id ASC LIMIT 10;
关于mysql - 访问 MySQL 中的最后 N-m 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15721077/