开发者

MySQL many to one get as string

Taking this example:

One table of persons [id, name].

One table of colours [id, colour].

One table of associations. (which colours are liked by which people) [id,person_id,colour_id].

I want to select all the persons with id and name as well as a column of the names of the colours each likes separated by comma a开发者_运维问答nd space: ", ". One person can like more than just one colour.

How should the query look?

Thank you.


Did I do good?

SELECT person.id, person.name, GROUP_CONCAT(colour.colour SEPARATOR ', ')
FROM person
JOIN associations ON associations.person_id = person.id
JOIN colours ON colours.id = associations.colour_id
GROUP BY person.id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜