我想从前一小时的最后一次记录中提取 la 值。 该表有:DateHour (TIMESTAMP) 和 Energy (FLOAT)
Id________DateHour_____________Energy
..........................................................................
18350____2019-10-05 18:57:44____231.38
18351____2019-10-05 18:59:45____231.33
..........................................................................
..........................................................................
18400____2019-10-05 19:51:00_____123.35
假设现在时间是 19:51:15。
我想选择上例中 ID 为 18351 的值。即使日子有变,我也想工作。
并列:
select *
from table1
where DateHour> (unix_timestamp()*1000 - 60*60*1000);
不好。
最佳答案
假设您正在使用 MySQL(代码如下):
select t1.*
from table1 t1
where datehour >= cast(date_format(now() - interval 1 hour, '%Y-%m-%d %h:00') as datetime) and
datehour < cast(date_format(now(), '%Y-%m-%d %H:00') as datetime)
order by datehour desc
limit 1;
MySQL 没有方便的函数(如 Postgres 中的 date_trunc()
)将日期/时间值向下舍入到特定单位。在本例中,它使用字符串转换来实现此目的。
请注意,查询的结构使得可以使用 datehour
上的索引。 。如果索引可用并且您的表很大,这一点很重要。
关于mysql - 上一小时的最后值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250465/