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

enter image description here

i want make league table this

enter image description here

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

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -