使用 MySQL 计算获胜次数和获胜次数最多的 MAX 参与者
我有一个名为 games
的表,其中包含以下字段:match
和 winner
。
Match
是matches
表的外键;winner
是“participants”表的外键,如果游戏仍未确定,则可以为NULL
。
当我选择比赛列表时,我需要一种方法来找出谁赢得了每场比赛,方法是计算每场比赛的获胜者并返回获胜次数最多的参与者(从获胜者字段中)。
子查询、GROUP BY
、COUNT
和 MAX
的各种组合尚未给我正确的结果。
我不认为我可以用 ORDER BY
和 LIMIT
代替 MAX
因为如果所有参与者都发生的话,这只会返回比赛中的第一个参与者获得相同数量的胜利。
I have a table called games
with the fields: match
and winner
.
Match
is a foreign key to thematches
table;winner
is a foreign key to the 'participants' table that can beNULL
if the game is still undecided.
I need a way to find out who won each match when I select a list of matches by counting who won each game and returning the participant (from the winner field) who had the most wins.
Various combinations of subqueries, GROUP BY
, COUNT
, and MAX
have yet to give me the right results.
I don't think I can substitute ORDER BY
and LIMIT
for MAX
because that would return just the first participant in a match if all participants happened to have the same number of wins.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过将 JOIN 与 GROUP BY 和 COUNT 一起使用?
约翰
Have you tried using JOINs with GROUP BY and COUNT ?
John