MySQL query with matching and sum - League Table -
i have 5 tables:
- players (id, name, team_id)
- teams (id, name)
- results (id, game_id, player.id, position)
- games (id, location)
- score (position, score)
as follows
i want make league table this
i confused how sum scores each player. code far:
select players.name, teams.name, results.position players inner join teams on players.team_id = teams.id join results on player.id = results.player_id join scores on results.position = scores.scores
any idea?
to list players (whether appear on results table or not) have use left join
.
select p.name player_name,t.name team_name,sum(coalesce(s.score,0)) total_score players p inner join teams t on t.id = p.team_id left join results r on p.id = r.player_id left join scores s on s.position = r.position group p.id order total_score desc
Comments
Post a Comment