我有一个包含列event_time
的表格。如何在NOW()之前选择两行,在NOW()之后选择下一行,由event_time
排序?
单个查询可以吗?
答案 0 :(得分:4)
像
这样的东西SELECT *
FROM (
SELECT *
FROM TABLE
WHERE event_time < NOW()
ORDER BY event_time DESC
LIMIT 2
) First2
UNION ALL
SELECT *
FROM (
SELECT *
FROM TABLE
WHERE event_time > NOW()
ORDER BY event_time ASC
LIMIT 1
) Next1
ORDER BY event_time