我有一张包含数千条记录的表:
[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做到这一点?
谢谢!
答案 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');