[英]MySQL WHERE condition to select all records from start of the hour
WHERE timestamp >= NOW() - INTERVAL 1 HOUR
The above query works fine to get data that belongs to last 1 HOUR.上面的查询可以很好地获取属于过去 1 小时的数据。 Let's say I run the query at 2020-10-15 18:08:00.
假设我在 2020-10-15 18:08:00 运行查询。 It will give me all the records between the below time period.
它会给我以下时间段之间的所有记录。
2020-10-15 17:08:00 - 2020-10-15 18:08:00
I have been trying to create a mysql query to get data from the beginning of the hour.我一直在尝试创建一个 mysql 查询以从一小时开始获取数据。 Let's again say I ran the query at 2020-10-15 18:08:00.
再次假设我在 2020-10-15 18:08:00 运行了查询。 I need a query to get me the results belonging the below time period.
我需要一个查询来获取属于以下时间段的结果。
2020-10-15 18:00:00 - 2020-10-15 18:08:00
I would appreciate some feedback on this.我将不胜感激对此的一些反馈。
In many databases (as Postgres for example), you can do this with date_trunc()
, or the-like (in Oracle: trunc()
).在许多数据库中(例如 Postgres),您可以使用
date_trunc()
或类似方法(在 Oracle 中: trunc()
)执行此操作。
Unfortunately MySQL does not implement this function - however it is flexible enough to understand dates as strings.不幸的是 MySQL 没有实现这个函数——但是它足够灵活,可以将日期理解为字符串。 I like to use
date_format
for this:我喜欢为此使用
date_format
:
where timestamp >= date_format(now(), '%Y-%m-%d %H:00:00')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.