My site lets users refer other users, and in some instances gives them reward points when a referred user does certain things.
I want to show the referring user (let's call her Amy, she has the user ID of 3) who she referred and how many points she was rewarded.
This query gets the users who Amy referred, plus the points they generated for her. It's working correctly.
SELECT users.username, users.created_at, SUM(reward_total)
AS total
FROM referral_points
JOIN users ON users.id = referral_points.user_id
WHERE referral_points.referred_by = 3
GROUP BY user_id
However, there are some users who signed up that didn't do anything to generate points for Amy, but I still want to show her who they are.
This query gets ALL of the users who Amy referred, regardless of points.
SELECT users.username, users.created_at, users.updated_at
FROM users
WHERE users.referred_by = 3
I'm having a difficult time using a UNION to join the queries. They produce results, but the results are duplicated, as I get the users from referral_points
AND the same users from users