I know this is similar to this question: MySQL Query - Sum one related table - Count another related table but I cannot make it work for me.
I have 3 tables:
Event
Attendee
Gifts
For each Event
we need to summarize the number of Attendees
, and the total amount of Gifts
. An Attendee
may, or may not, receive a gift.
I can get it working so that if I ignore the gift totals, I get the right answer with this query:
SELECT event.name AS name, count( * ) AS num_attendee
FROM attendee
RIGHT JOIN event on event.id = attendee.event_id
WHERE event.company_id =6
GROUP BY event.id
But I have no idea how to sum the gifts (i.e. Gift.Amount
values) given to all Attendees
for each event. When I try to simply join in the Gift
table based upon event_id
all the numbers go wacky.