MYSQL GROUP BY MAX得分

时间:2014-04-21 06:34:51

标签: mysql

我有一个名为score的表,其中包含列

enter image description here

如何选择哪个id_team是每场比赛的最佳射手?

我正在尝试这个,但那不是正确的结果

SELECT MAX( score ) , id_team
FROM scores
GROUP BY  `id_game` 
LIMIT 0 , 30

2 个答案:

答案 0 :(得分:2)

您可以使用自我加入来找出具有最高分数

的游戏的正确团队ID
SELECT s.* 
FROM scores s
JOIN (
SELECT MAX(score) score, id_game 
FROM scores
GROUP BY id_game ) ss USING(score ,id_game )
LIMIT 0 , 30

答案 1 :(得分:0)

select A.id_game, A.id_team as winning_team
from scores A,
(
select max(score) as max, id_game
from scores
group by id_game
)B
where A.id_game = B.id_game
and A.score = B.max