【发布时间】:2019-08-27 17:15:34
【问题描述】:
我有一个这样的查询,给出分数和排行榜首字母:
select MAX(score) as score, leaderboard_initials
from players p, games g where p.google_id = g.google_id
group by p.google_id
order by MAX(score) DESC;
Players 有一个主键 google_id,它是 games 中的外键。
它有效。
我需要显示玩家的排名,其中考虑了他们的最高得分游戏。
我在想,对于排名,我需要 1 + 该玩家之上的玩家数量。因此,该玩家的最高得分高于该玩家。因此,我尝试了以下操作,但收到错误 invalid use of group function:
select 1+(SELECT count(DISTINCT p2.google_id) from players p2, games
g2 where MAX(g2.score) > score) as rank,
MAX(score) as score, leaderboard_initials
from players p, games g where p.google_id = g.google_id
group by p.google_id
order by MAX(score) DESC;
我知道我不能在WHERE 中使用MAX(),但如果不这样做,我不知道如何获得排名。有任何想法吗?
【问题讨论】:
-
哪个版本的 MySQL?
-
@Uueerdo
5.6.40-84.0-log
标签: mysql