我正在尝试执行一个查询,该查询每时获取一次数据,hour但不是 按小时分组,而是想按小时缩小范围,仅获取latest小时- 表示该小时内的最新数据。下面显示的图片是我 想要得到的带有红色框的行。如果你会发现,第一个红色行是 10:59:51它的,这意味着只有一行的中10:00:00和 10:59:59。对于其余的行,12:00我想 获取,12:37:14因为它是该小时范围内的最新行或最新行。
hour
latest
10:59:51
10:00:00
10:59:59
在此处输入图片说明
我有一个简单的查询,它通过hour使用HOUR()如下方式对数据进行分组:
HOUR()
SELECT userid, username, date_created FROM user_accounts WHERE date_created >= '2009-10-27 00:00:00' AND date_created < '2009-10-27 23:59:59' GROUP BY HOUR(date_created)
The query, however, is just grouping it by hour 10 and 12 which returns id 24 and 25 - but what I needed is id 24 and 28. Any ideas?
10
12
24
25
28
Try
SELECT f.* FROM ( SELECT MAX(UNIX_TIMESTAMP(date_created)) AS mts FROM user_accounts GROUP BY HOUR(date_created) ) s JOIN user_accounts f ON UNIX_TIMESTAMP(date_created) = s.mts WHERE DATE(date_created) = '2009-10-27'