我正在使用以下查询:
SELECT
cm.id, name, role_type
FROM
main_creditmaster cm INNER JOIN main_person p on cm.person_id=p.id
WHERE
tv_series_id=8 order by name
这给了我以下结果:
id name role_type
5890 Daniel Dae Kim 0
20764 Evangeline Lilly 0
57271 Jorge Garcia 0
57271 Jorge Garcia 1
我想对person_id进行分组,但我还希望显示用户拥有的所有role_types
。我想得到的最终结果如下,“Jorge Garcia”的角色合并为一个条目:
id name role_type
5890 Daniel Dae Kim 0
20764 Evangeline Lilly 0
57271 Jorge Garcia 0,1
我该怎么做?
答案 0 :(得分:2)
您需要group by
和group_concat()
:
SELECT cm.id, name, GROUP_CONCAT(role_type) as roles
FROM main_creditmaster cm INNER JOIN
main_person p
on cm.person_id = p.id
WHERE tv_series_id = 8
GROUP BY cm.id, name
order by name