Question

My current query below gets all the judges who signed up for events. The subquery in the in clause is because an event can be one or more days and the judges can sign up for one or more. This is fine but if its a 2 day event and the judge signs up for both, their are 2 rows return for the user. I want to combine them so it shows their info plus both event days. How would I go about that?

select concat(ej.FirstName, ' ', ej.LastName) as judge, ej.email, ej.phone, ed.EventDate
from EventJudges ej 
inner join EventJudgeXref ejx on ej.Id = ejx.EventJudgeId 
inner join EventDates ed on ejx.EventDateId = ed.Id
where ejx.EventDateId in(SELECT Id FROM EventDates WHERE EventId = 1) 
order by ej.FirstName asc, ej.LastName asc

The EventJudgeXref table contains a JudgeId and an EventDateId for reference.

Was it helpful?

Solution

Try using group_concat:

select concat(ej.FirstName, ' ', ej.LastName) as judge,
    ej.email,
    ej.phone,
    group_concat(ed.EventDate) as EventDate
from EventJudges ej 
inner join EventJudgeXref ejx on ej.Id = ejx.EventJudgeId 
inner join EventDates ed on ejx.EventDateId = ed.Id
where ejx.EventDateId in(SELECT Id FROM EventDates WHERE EventId = 1) 
group by ej.Id
order by ej.FirstName asc, ej.LastName asc

SQL Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top