这是this问题的后续内容。
我希望获得玩家获得最高分的游戏ID。我在选择max(得分)时难以获得相应的game_id列值
我的表格如下:
id game_id player_id score
1 1 1 345
2 1 2 234
3 2 1 190
4 2 2 167
5 3 4 230
6 3 1 230
7 4 2 453
8 4 3 230
到目前为止,我的查询看起来像这样:
SELECT s.id, t.game_id, t.score
FROM (
SELECT game_id, score
FROM stats
WHERE player_id =2
) t
LEFT JOIN stats s on s.game_id = t.game_id AND s.score = t.score
WHERE s.player_id = 2
这给了我
id game_id score
2 1 234
4 2 167
7 4 453
然后我需要将整个查询作为子查询加入最高分(id = 7),然后获取game_id,但我不确定如何说这个最大值的加入
答案 0 :(得分:0)
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT player_ID, MAX(Score) max_score
FROM TableName
GROUP BY Player_ID
) b ON a.Player_ID = b.player_ID AND
a.Score = b.max_score
答案 1 :(得分:0)
这里我创建了sql feedle你可以删除
SELECT s.id, t.game_id, t.score
FROM (
SELECT game_id, score
FROM game
WHERE player_id =2 order by score desc limit 0,1
) t
LEFT JOIN game s on s.game_id = t.game_id AND s.score = t.score
答案 2 :(得分:0)
希望这可以帮到你。
select game_id, player_id, score
from (
select rank() over (partition by player_id order by score desc) as rank_id, game_id, player_id, score
from stats
) a
where rank_id=1 and player_id = 2