<分区>
当我们有两个具有一对多关系的表时,我们如何获得左表列的总和。
Ex:
Player (player_id,name)
Stats (player_id, game_id, score)
Player
1 Aaron Brooks
2 Beno Udrih
3 James harden
Stats
player_id score year
1 5 2017
1 3 2017
2 4 2016
The result I need to get is (Player scores in 2017)
Player Score
Aaron Brooks 8
Beno Udrih 0
James harden 0
一个球员有很多数据记录。
那么我怎样才能得到所有球员的名单以及他的总得分总和。 (即使某个球员的统计数据不可用,他的分数也需要为 0)
Got the correct query
select p.player_id,p.name,coalesce(sum(s.score),0)
from player p
left join stats s on p.player_id = s.player_id and s.year=2017
group by p.player_id,p.name