SQL 选择 table 中具有最大值的行,该行由另一列排序
SQL selecting the rows with the maximum value in a table ordered by a column from another
我有 2 个表格,简化后如下所示:
Name Server_id score
-----------------------------
John 1 300
John 2 400
Mary 2 321
John 1 100
Mary 1 50
Mary 2 10
Server_id game
-------------------
1 pong
2 Mario
每个玩家可以拥有与任何服务器关联的多个分数。而一个Server,对应一个游戏。
现在我想执行一个 select 语句,其中 returns 玩家在每场比赛中的最高得分。像这样:
Name game score
-----------------------
John pong 300
John Mario 400
Mary pong 50
Mary Mario 321
除非我遗漏了什么,否则这只是一个 JOIN
和 GROUP BY
:
select t1.name, t2.game, max(t1.score)
from table1 t1 join
table2 t2
on t1.server_id = t2.server_id
group by t1.name, t2.game;
我有 2 个表格,简化后如下所示:
Name Server_id score
-----------------------------
John 1 300
John 2 400
Mary 2 321
John 1 100
Mary 1 50
Mary 2 10
Server_id game
-------------------
1 pong
2 Mario
每个玩家可以拥有与任何服务器关联的多个分数。而一个Server,对应一个游戏。
现在我想执行一个 select 语句,其中 returns 玩家在每场比赛中的最高得分。像这样:
Name game score
-----------------------
John pong 300
John Mario 400
Mary pong 50
Mary Mario 321
除非我遗漏了什么,否则这只是一个 JOIN
和 GROUP BY
:
select t1.name, t2.game, max(t1.score)
from table1 t1 join
table2 t2
on t1.server_id = t2.server_id
group by t1.name, t2.game;