繁体   English   中英

SQL从另一个列排序的表中选择具有最大值的行

[英]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

每个玩家可以有多个与任何服务器相关联的分数。 而对于一个服务器,对应一个游戏。

现在我想执行一个 select 语句,它在每场比赛中返回玩家的最高分数。 像这样的东西:

Name     game     score
-----------------------
John     pong     300
John     Mario    400
Mary     pong     50
Mary     Mario    321

除非我遗漏了什么,这只是一个JOINGROUP 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM