0

这是这个问题的后续。

我想获取玩家得分最高的游戏 id。在选择 max(score) 时,我正在努力获取相应的 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,但我不知道怎么说加入这个最大值

4

3 回答 3

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
于 2013-03-15T16:41:17.367 回答
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
于 2013-03-15T17:25:31.137 回答
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

于 2013-03-15T17:40:45.733 回答