【问题标题】:MySQL count how many records has user madeMySQL计算用户创建了多少条记录
【发布时间】:2020-08-09 12:22:58
【问题描述】:

我正在尝试计算唯一玩家 [auth] 的记录数。 所以基本上我试图在每张地图上选择最短的时间并计算剩下的条目最多的人。

我通过大量的谷歌搜索走了这么远。

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

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

我更喜欢这种类型的回答查询:

[auth] [records_made]

【问题讨论】:

标签: mysql sql group-by greatest-n-per-group min


【解决方案1】:

我认为你想要:

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
    )

这为您提供了每个auth 拥有最少时间的maps 的数量(据我了解,这是您对创建记录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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2014-05-14
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多