Tom
Tom

Reputation: 37

MYSQL sum() returning double score

I'm using the following query to produce a leaderboard, but for some reason it's returning the score as double the value.

SELECT p.user_id, u.first, SUM(points) AS score FROM points AS p LEFT 
JOIN users AS u ON p.user_id = u.fb_id WHERE p.action_time > 
'1492732800' GROUP BY p.user_id ORDER BY points DESC LIMIT 10

What might be causing this to happen?

Thanks

Upvotes: 0

Views: 632

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

One workaround is to aggregate the points in a separate subquery and join to that:

SELECT t1.fb_id, t1.first, t2.score
FROM users t1
INNER JOIN
(
    SELECT user_id, SUM(points) AS score
    FROM points
    WHERE action_time > '1492732800'
    GROUP BY user_id
) t2
    ON t2.user_id = t1.fb_id

Upvotes: 1

Related Questions