我每五分钟收集一次数据并将其输入MySQL数据库。我想提取最近超过2小时的行,然后是超过1小时的行,接着是最近一行。
我正在考虑下面的内容,但我认为这会让我每小时的最后一行。这意味着如果我在8:05运行查询,我可能会从6:57,7:57和8:02返回行,其中最后2行距离不到一小时。
SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY Date DESC
GROUP BY HOUR(Date) LIMIT 3
) x
ORDER BY Date ASC
感谢您提供任何帮助或建议。
答案 0 :(得分:1)
SELECT * FROM (
SELECT * FROM mytable
WHERE `Date`<DATE_SUB(NOW(), INTERVAL 2 HOUR)
ORDER BY DATE DESC LIMIT 1
)
UNION
SELECT * FROM (
SELECT * FROM mytable
WHERE `Date`<DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY DATE DESC LIMIT 1
)
UNION
SELECT * FROM (
SELECT * FROM mytable
ORDER BY DATE DESC LIMIT 1
)
ORDER BY `Date`ASC