I have 2 different tables whose purpose is to store the points gotten by a group of users in a football game contest.
The 1st table stores the points gotten by the user for a prediction of the team players who will eventually play a game, and the 2nd one stores the points for a prediction of the final score of the game.
So both tables will store as many rows as users who predict, times as many games that have been played or to be played (one row of each football game for each user).
An example of how indistinctly one of these tables might look supposing that there are only three users and only three games:
Code:
userid gameid status points
-----------------------------------------------
1 1 1 20
1 2 1 24
1 3 1 15
2 1 1 18
2 2 1 22
2 3 1 16
3 1 1 12
3 2 1 8
3 3 1 17
The other table would look similarly but will store different points.
What I want to get is a ranking with the total score of every user by adding up their stored points. I've been trying with several queries but I don't seem to get the expected result. For instance, this is the query I considered more accurate to what I wanted, but the totals are much bigger than expected:
SELECT ROUND( SUM(p.points) + SUM(r.points)) AS total, p.userid
FROM prediction_players p
INNER JOIN prediction_results r
ON p.userid = r.userid AND status=1
GROUP BY a.userid
ORDER BY total DESC
Note: the status field stored whether the corresponding game has already been played (1) or not (0). It must therefore be = 1 because otherwise points=NULL
|