I have a user table (User) and 3 tutorial tables (Text, Video and Other).
Each tutorial has the columns rating_positive
and rating_negative
and is linked to a user (id).
I want to select the 10 users with the most tutorials and the sum of positive/negative ratings of their tutorials.
I tried the following query but it does not work. It returns way too many results for tutorials_count/pos/neg. How can I do it correctly?
SELECT
u.id AS user_id,
(COUNT(t.id) + COUNT(v.id) + COUNT(o.id)) AS tutorials_count,
(SUM(t.rating_positive) + SUM(v.rating_positive) + SUM(o.rating_positive)) AS pos,
(SUM(t.rating_negative) + SUM(v.rating_negative) + SUM(o.rating_negative)) AS neg
FROM
user u LEFT JOIN trick t ON u.id = t.submitter_id
LEFT JOIN video v ON u.id = v.submitter_id
LEFT JOIN other o ON u.id = o.submitter_id
GROUP BY u.id
ORDER BY tutorials_count DESC
LIMIT 10