sql double inner join:使用group_concat给出空结果集

时间:2013-01-23 10:01:25

标签: mysql yii

我有一个表education,其中包含一个人员ID和一个教育级别ID,两个外键。我想对结果进行分组,以便每个人记录都是单数,包含所有教育水平的列表,因此它返回 Jane Doe:master,master

select 
  concat(person.name_first," ",person.name_last) as name,
  group_concat(person_studylevel.description)
from `person_education`
left join person_studylevel on level = person_studylevel.id
left join person on junior = person.id

这一直有效,直到我添加了group_concat(),然后它返回一个空的结果集

我已尝试将结果用作子查询,但也没有效果

表格结构:

person_education: id(int10), junior(int10), level(int10)
person_studylevel: id(int10), description(varchar100)
person: id(int10), name_first(varchar255), name_last(varchar255)

1 个答案:

答案 0 :(得分:1)

请检查一下,如果你想按每个人分组,那么把那张桌子当作左边第一个:

查询:

select 
  concat(p.name_first," ",p.name_last) as name,
  group_concat(ps.description)
from person p 
left join person_education pe
on p.id = pe.junior
left join person_studylevel ps
on pe.level = ps.id
group by p.id
;

| NAME | GROUP_CONCAT(PS.DESCRIPTION) |
---------------------------------------
|  j k |               degree,masters |
|  a g |               diploma,degree |
|  b h |                         cert |
|  c i |                       (null) |
|  d m |                       (null) |