Avatar of jej07
jej07
 asked on

Help building a query for a pivot table.

I have two tables, one for events and one for registrations. What's the best way to count how many total registrations I have so they can be displayed as seen under "Results"?

events
id_events       | short_description
2            | Event 1
15            | Event 2
35            | Event 3
      
registration_xref
event_id       | attendee_id
2            | 52
2            | 65
2            | 74
15            | 65
35            | 65
35            | 74

Results
Description      Registered
Event 1            3
Event 2            1
Event 3            2

This was my latest attempt.

$query = mysql_query("
	SELECT events.id_events, events.short_description,
	COUNT(CASE WHEN registration_xref.event_id='2' then 1 END) onecount,
	COUNT(CASE WHEN registration_xref.event_id='15' then 1 END) twocount,
	COUNT(CASE WHEN registration_xref.event_id='35' then 1 END) threecount				
	FROM events 
	LEFT JOIN registration_xref ON registration_xref.event_id = events.id_events AND event_id in ('2','15','35')
	GROUP BY events.id_events, events.short_description");

Open in new window

PHPMySQL ServerSQL

Avatar of undefined
Last Comment
jej07

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ralmada

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jej07

ASKER
That's exactly what I was trying to accomplish. Thank you!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23