troubleshooting Question

Help building a query for a pivot table.

Avatar of jej07
jej07 asked on
PHPMySQL ServerSQL
2 Comments1 Solution369 ViewsLast Modified:
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");
ASKER CERTIFIED SOLUTION
ralmada

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros