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

发布于 2024-11-28 05:13:07 字数 734 浏览 0 评论 0原文

我有一个包含数千条记录的表,如下所示:

[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来做到这一点?

谢谢!

I have a table with thousands of records like this:

[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

I want to group the data in minutes and find the maximum score of each group.

So the result is like this:

[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

How can I do this through MySQL?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-12-05 05:13:07
select 
date_format(actiontime,'%Y-%m-%d %H:%i:00') as act_time,
max(score) as greater
from table
group by act_time
select 
date_format(actiontime,'%Y-%m-%d %H:%i:00') as act_time,
max(score) as greater
from table
group by act_time
反话 2024-12-05 05:13:07

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

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

You don't need to select the formatted field, you can use it directly in GROUP BY.

SELECT * FROM table_name
GROUP BY date_format(datetime_field, '%Y-%m-%d %H:%i:00');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文