mysql GROUP_CONCAT

*爱你&永不变心* 提交于 2019-12-04 05:29:43

This is not the best design, but here's the query you want:

SELECT  user_id, GROUP_CONCAT(class_name)
FROM    users
JOIN    classes
ON      FIND_IN_SET(class_id, user_class)
GROUP BY
        user_id

This breaks rules of database normalization. Don't store many-to-many values in a comma-separated list. Instead make another table user_classes with fields user_id and class_id. This table is simply used as a link table between the other two classes. After this you can use GROUP_CONCAT to do what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!