更改当前的MySql查询以基于id组合结果

时间:2014-05-12 23:23:28

标签: mysql select

我在下面的当前查询将获得所有注册参加活动的评委。该 in子句中的子查询是因为一个事件可以是一天或多天而且 评委可以报名参加一个或多个。这很好,但如果它是一个2天的事件和法官 注册两者,他们为用户返回2行。我想将它们结合起来,以便显示它们的信息以及两个事件日。我该怎么做呢?

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

EventJudgeXref表包含JudgeId和EventDateId供参考。

1 个答案:

答案 0 :(得分:5)

尝试使用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