简体   繁体   English

在过去24小时内按时间戳过滤结果

[英]Filter results by timestamp for the past 24 hours

I am trying to filter the count of Rows found, per hour, over the past 24 hours. 我正在尝试过滤过去24小时内每小时发现的行数。

I am using the following SQL query and 'landtime' is a DateTime field. 我正在使用以下SQL查询,并且“ landtime”是DateTime字段。

SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > DATE_SUB(NOW(), INTERVAL 24 HOUR) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)

Sample data set > http://pastebin.com/0rYBnePG 样本数据集> http://pastebin.com/0rYBnePG

The results looked as expected, until I looked at the data it was using. 结果看起来像预期的一样,直到我查看了它使用的数据。

It was pulling dates from days/weeks before todays date when I only want a count for the past 24 hours. 当我只想要过去24小时的计数时,它是从今天日期之前的几天/几周中提取日期。

Any help appreciated! 任何帮助表示赞赏!

SELECT * FROM table WHERE day(data) = EXTRACT(day FROM (NOW() - INTERVAL 1 day))

无需使用函数,只需使用简单的数学运算即可:

SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > (CURRENT_TIMESTAMP - INTERVAL 1 DAY) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM