MySQL query with matching and sum - League Table

Ally

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 to make a league table like this

enter image description here

I am confused how to sum up the scores for each player. This is my code so far:

SELECT Players.name, Teams.name, Results.position FROM 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?

Juan Pablo Califano

To list all the players (whether they appear on the results table or not) you have to use a LEFT JOIN.

SELECT p.name player_name,t.name team_name,SUM(COALESCE(s.score,0)) as total_score
FROM 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 BY p.id
ORDER BY total_score desc 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL league table

From Dev

How to order a sports league table in MySQL?

From Dev

MySQL query, sum data from table

From Dev

MySQL: Update all rows in 2 table matching results of another query

From Dev

Optimal MYSQL query for longest prefix matching in a table with 5 million rows

From Dev

MySQL Join; Query only non-matching table fields

From Dev

Optimal MYSQL query for longest prefix matching in a table with 5 million rows

From Dev

MYSQL Query matching column names to rows in another table

From Dev

Mysql query for sum two table data by product wise

From Dev

MySQL Sum and Case Query

From Dev

Mysql query SUM group?

From Dev

MySQL query entire table using 1 of 2 given matching parameters in first query

From Dev

Mysql query sum query in codeigniter

From Dev

MYSQL: Return all rows in one table along with the sum of matching rows in another

From Dev

Display a league table on a webpage?

From Dev

MySQL sum with multiple table

From Dev

PDO SUM MYSQL table

From Dev

MySQL multiple table sum

From Dev

MySQL query not matching due to punctuation

From Dev

Fast String Matching MySQL Query

From Dev

Pattern matching in MySQL database query

From Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

From Dev

How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

From Dev

How to get the sum of the sum with one mysql query

From Dev

Squash League Results - SQL Query

From Dev

Need help on MYSQL QUERY (SUM)

From Dev

MySQL query to get the sum of a column

From Dev

MySQL: Sum () and Count in one query?

From Dev

Mysql query sum and find average

Related Related

  1. 1

    MySQL league table

  2. 2

    How to order a sports league table in MySQL?

  3. 3

    MySQL query, sum data from table

  4. 4

    MySQL: Update all rows in 2 table matching results of another query

  5. 5

    Optimal MYSQL query for longest prefix matching in a table with 5 million rows

  6. 6

    MySQL Join; Query only non-matching table fields

  7. 7

    Optimal MYSQL query for longest prefix matching in a table with 5 million rows

  8. 8

    MYSQL Query matching column names to rows in another table

  9. 9

    Mysql query for sum two table data by product wise

  10. 10

    MySQL Sum and Case Query

  11. 11

    Mysql query SUM group?

  12. 12

    MySQL query entire table using 1 of 2 given matching parameters in first query

  13. 13

    Mysql query sum query in codeigniter

  14. 14

    MYSQL: Return all rows in one table along with the sum of matching rows in another

  15. 15

    Display a league table on a webpage?

  16. 16

    MySQL sum with multiple table

  17. 17

    PDO SUM MYSQL table

  18. 18

    MySQL multiple table sum

  19. 19

    MySQL query not matching due to punctuation

  20. 20

    Fast String Matching MySQL Query

  21. 21

    Pattern matching in MySQL database query

  22. 22

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  23. 23

    How to generate a query which consider data from one table and correspondingly fill 0 for non matching fields in mysql

  24. 24

    How to get the sum of the sum with one mysql query

  25. 25

    Squash League Results - SQL Query

  26. 26

    Need help on MYSQL QUERY (SUM)

  27. 27

    MySQL query to get the sum of a column

  28. 28

    MySQL: Sum () and Count in one query?

  29. 29

    Mysql query sum and find average

HotTag

Archive