2011-08-08 47 views
2

我有這樣的數千條記錄的表:MySQL的:按日期時間記錄,而忽略了「秒」

[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的?

謝謝!

回答

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

值得注意的是,如果你用printf格式化查詢(或此功能的一些衍生產品),逃生即「%Y」把「%% Y」(或printf的會盡量把論點在放置'%%'將它視爲普通字符串)。 – therealszaka

1

您不需要選擇格式化字段,您可以直接在GROUP BY中使用它。

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