I have this calculation/query here:
SELECT u.username,
(a.totalCount * 7) +
(b.totalCount * 3) +
(c.totalCount * 1) AS totalScore
FROM users u
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM items
GROUP BY user_id
) a ON a.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM comments
GROUP BY user_id
) b ON b.user_id= u.user_id
LEFT JOIN
(
SELECT user_id, COUNT(user_id) totalCount
FROM ratings
GROUP BY user_id
) c ON c.user_id = u.user_id
ORDER BY totalScore DESC LIMIT 10;
The problem is, if either a,b, or c returns 0, the entire totalScore is 0. I can’t figure out what is going on? I am not multiplying the final tally by 0 I don’t think?
I think it’s rather a null problem (with your LEFT JOIN, this might easily happen).
And
NULL + 1 + 2 = NULLSo use the
COALESCE(if null then…) operatorso
and a little SqlFiddle