Iris Louis Iris Louis - 10 months ago 106
SQL Question

Group concat query mysql

I had results after query success

table user :

enter image description here

And with results. I want join to table subjects get subject name by id.

table Subjects :

enter image description here

Id of table user related with table Subjects

Output I want :

enter image description here

Can you give me query. Thanks all !


You're storing CSV data in your MySQL table, which is not desirable. But one way you can work with it is to join the Subjects table to the user table using FIND_IN_SET to handle the CSV data contained in the latter.

SELECT t2.defaultSubjectId,
FROM Subjects t1
INNER JOIN user t2
    ON FIND_IN_SET(t1.subjectId, t2.defaultSubjectId) > 0
GROUP BY t2.defaultSubjectId

Demo here: