我想在数据中获取一个小时前的最新旧值,但在执行此查询时

SELECT locid, value
FROM  table2
WHERE date_sub(t_stamp, interval 1 hour)
and locid = '2815'
order by t_stamp desc

我得到所有的价值观。我该怎么解决?
输出应该是
locid  | value
2815   | 13.0

演示如下:http://sqlfiddle.com/#!2/b3c89/5

最佳答案

以下查询将获取当前小时并从中减去一,然后将其与t_stamp的小时进行比较。

SELECT locid, value
FROM  table2
WHERE DATE(t_stamp) = DATE(CURRENT_DATE)
AND HOUR(t_stamp) = HOUR(CURRENT_TIME) -1
AND locid = '2815'
ORDER BY t_stamp desc
LIMIT 1

或者以下查询可能是您要查找的内容:
SELECT locid, value
FROM  table2
WHERE t_stamp <= DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND locid = '2815'
ORDER BY t_stamp desc
LIMIT 1

关于mysql - 从一小时前的mysql中获取最新的较早的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20226357/

10-08 22:05