2

我有一个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)
4

1 回答 1

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) |
于 2013-01-23T11:00:28.917 回答