I'm currently using a points system that uses the query below to list who has the most points in a category or any category directly below it (using 'links_to' and 'links_from' in the 'category_relations table').
SELECT u.name,
pa.user_id,
SUM(IF(pa.plus = '1', pa.points_amount, 0)) -
SUM(IF(pa.plus = '0', pa.points_amount, 0)) AS points
FROM points_awarded pa, Users u
WHERE u.user_id = pa.user_id AND
(
category_id = '" . $category_id . "' OR
category_id IN
(
SELECT links_to
FROM category_relations
WHERE links_from = '" . $category . "'
)
)
GROUP BY user_id
ORDER BY points DESC
LIMIT 50
What I'm now looking to do is switch this query to look for all categories for a specific user instead of looking for all users for a specific category. Below is the query i've tried to use, but this only includes points for the category and not sub categories directly below.
SELECT u.name,
pa.user_id,
pa.category_id,
SUM(IF(pa.plus = '1', pa.points_amount, 0)) -
SUM(IF(pa.plus = '0',pa.points_amount, 0)) AS points
FROM points_awarded pa, Users u
WHERE u.user_id = pa.user_id AND
u.user_id = '" . $user_id . "'
GROUP BY pa.category_id
ORDER BY points DESC
LIMIT 50