mysql从表中的前50条记录中选择最后5条记录

时间:2016-11-12 12:09:40

标签: php mysql

我想从表格中的前50条记录中选择最后5条记录,目前我有以下查询,有人告诉我选择这些记录的最佳方法而不计算限额和偏移量吗?

SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC 

答案 1 :(得分:0)

这有效:

  

SELECT id FROM(SELECT id,date FROM table ORDER BY date LIMIT 50)AS   可订购的ORDER BY日期DESC LIMIT 5