MySQL:按日期时间分组记录忽略“秒”

时间:2011-08-08 12:17:58

标签: mysql date group-by

我有一张包含数千条记录的表:

[Row]    ActionTime              Score
=======================================
[#1]   2011-08-06 12:30:15       30
[#2]   2011-08-06 12:30:20       50
[#3]   2011-08-06 12:30:47       40
[#4]   2011-08-06 12:40:12       30
[#5]   2011-08-06 12:40:28       10
[#5]   2011-08-06 12:45:12       60

我想在几分钟内对数据进行分组,并找到每个组的最高分数。

所以结果是这样的:

[Row]  ActionTime (without "second")          Score        
========================================================
[#1]   2011-08-06 12:30:00                     50  
[#2]   2011-08-06 12:40:00                     30
[#3]   2011-08-06 12:45:00                     60

我如何通过MySQL做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:6)

select 
date_format(actiontime,'%Y-%m-%d %H:%i:00') as act_time,
max(score) as greater
from table
group by act_time

答案 1 :(得分:1)

您无需选择格式化字段,可以直接在GROUP BY

中使用
SELECT * FROM table_name
GROUP BY date_format(datetime_field, '%Y-%m-%d %H:%i:00');