I am making a scoreboard for my internship, for the games we play in the break. I have a really big query to get the results of all games and count the total of wins by players.
SELECT winner, SUM(total) AS total FROM (SELECT pw.name AS `winner`, COUNT(*) AS total FROM billiard_games g INNER JOIN players p1 ON p1.id = g.player_1 INNER JOIN players p2 ON p2.id = g.player_2 LEFT JOIN billiard_winners w ON w.id = g.id LEFT JOIN players pw ON pw.id = w.winner WHERE winner IS NOT NULL GROUP BY winner UNION ALL SELECT pw.name AS `winner`, COUNT(*) AS total FROM dart_games g INNER JOIN players p1 ON p1.id = g.player_1 INNER JOIN players p2 ON p2.id = g.player_2 LEFT JOIN dart_winners w ON w.id = g.id LEFT JOIN players pw ON pw.id = w.winner GROUP BY winner UNION ALL SELECT pw.name AS `winner`, COUNT(*) AS total FROM fifa_games g INNER JOIN players p1 ON p1.id = g.player_1 INNER JOIN players p2 ON p2.id = g.player_2 LEFT JOIN fifa_winners w ON w.id = g.id LEFT JOIN players pw ON pw.id = w.winner WHERE winner IS NOT NULL GROUP BY winner ) A WHERE winner IS NOT NULL group by winner DESC LIMIT 5
Output:
Player_1 | 5 Player_2 | 1 Player_3 | 3
That’s not the right order. Is there something wrong?
Advertisement
Answer
Last part should be
WHERE winner IS NOT NULL group by winner order by total DESC LIMIT 5
Because you just missed the ORDER BY