MySQL计算用户已完成多少记录

问题描述 投票:0回答:1

[我正在尝试计算有多少条记录是由唯一的玩家[​​auth]完成的。因此,基本上,我正在尝试在每张地图上选择最短的时间,并计算出剩下的条目最多的人。

我在使用谷歌搜索功能时走得很远。

SELECT
    auth,
    map,
    MIN(time) AS Record
FROM
    bhop.playertimes
WHERE track LIKE '0'
GROUP BY
    map
)

这成功列出了每张地图的最高记录时间和记录的[auth]。汇总[auth]条目的最简单方法是什么?

我希望这种类型的答案查询:

[auth] [records_made]

Database structure

Query result

mysql sql group-by greatest-n-per-group min
1个回答
0
投票

我认为您想要:

select p.auth, count(*) no_records
from bhop.playertimes p
where 
    p.track = 0
    and p.time = (
        select min(p1.time) 
        from bhop.playertimes p1 
        where p1.map = p.map and p1.track = 0
    )

这给您每map个拥有最短时间的auth个数(据我所知,这是您对[所定义的auth的定义。

如果您正在运行MySQL 8.0,则可以通过窗口函数rank()获得相同的结果:

select auth, count(*) no_records from ( select auth, rank() over(partition by map order by time) rn from bhop.playertimes where track = 0 ) p group by auth

© www.soinside.com 2019 - 2024. All rights reserved.