MySQL按用户分组时选择最高单圈时间 [英] MySQL Select top lap times while grouped by user

查看:57
本文介绍了MySQL按用户分组时选择最高单圈时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 MySQL 表来记录单圈时间.组成如下

I have a simple MySQL Table for lap times. Made up of the following

  • 主键
  • 跟踪 ID
  • 用户 ID
  • 单圈时间
  • 单圈状态

我基本上想选择前 10 圈时间,但每个用户只能选择一个.所以如果我在 X 赛道上有 2 圈时间,我想获得最好的.

I basically want to select the top 10 lap times, but only one per user. So if I have 2 lap times for X track, I want to get the best one.

如果我跑

SELECT * FROM tbl_lap_times ORDER BY lap_times ASC

我看到了我的单圈时间,我最好的时间在顶部.但正如前面提到的,我只想要每个用户一圈时间.

I see both my lap times, with my best time at the top. But as mentioned, I only want one lap time per user.

如果我跑

SELECT * FROM tbl_lap_times GROUP BY users_id ORDER BY lap_times ASC

然后我得到了我的第一圈时间,(这不是我最好的单圈时间)

Then i get my first lap time, (which is not my best lap time)

有什么建议吗?

推荐答案

您需要使用 MIN 进行聚合.像这样的东西应该可以工作(未经测试):

You need to aggregate using MIN. Something like this should work (untested):

SELECT user_id, track_id, lap_status, MIN(lap_times) AS best_time
FROM tbl_lap_times
GROUP BY user_id, track_id, lap_status
ORDER BY best_time DESC
LIMIT 10

这篇关于MySQL按用户分组时选择最高单圈时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆