我需要选择40行,其中包含今天的日期和10个日期较旧的记录,按日期排序。
如果MySQL支持负偏移,它将如下所示:
SELECT * FROM `mytable` WHERE `date` >= '2013-10-29' ORDER BY date LIMIT -10, 40;
不支持负偏移。我该如何解决这个问题?感谢!!!
答案 0 :(得分:3)
使用UNION
组合两个查询:
(
SELECT *
FROM mytable
WHERE date < '2013-10-29'
ORDER BY date DESC
LIMIT 10
) UNION ALL (
SELECT *
FROM mytable
WHERE date >= '2013-10-29'
ORDER BY date
LIMIT 40
)
ORDER BY date -- if results need to be sorted