0

I have a MySQL database with 3 tables.

  • Table player

P_id = content (1, 2, 3,)

P_name = content (Spieler1name, Spieler2name, Spieler3name)
  • Table tournament

T_id = content (1, 2)

T_date = content (2018, 2019)

T_players = content (123, 123)
  • Table Games

G_id = content (1, 2)

G_tournament_id = content (T_id from table tournament)

G_player_away = Content (as an example user id 1)

G_player_home = content (as an example user id 2)

G_player_away_points = Content (11)

G_player_home_points = Content (8)

The Games table has even more data.

Now I want to create a table with PhP as ranking.

Place name points

  1. Spieler1name 11

  2. Spieler2name 8

  3. Spieler3name 4

  4. ....

Can someone tell me how the php query from the mysql database works? I tested this but it doesn't work.

SELECT SUM(G_player_away_points) AS total FROM Games 
UNION
SELECT SUM(G_player_home_points) AS total FROM Games
GROUP BY G_player_away

In the code above, he also sums the points from the opponent. This is of course not correct, it should only the points obtained by him appear in the output with php table. Furthermore you have to get the name from the table players. Can someone help me?

1 Answer 1

1

If you are using single table and multiple column for sum then you should try with this

SELECT SUM(G_player_away_points + G_player_home_points) AS total FROM Games 
GROUP BY G_player_away
Sign up to request clarification or add additional context in comments.

1 Comment

Also you should be careful for null values as if one of G_player_away_points or G_player_home_points is null, the resulting sum will be null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.