I have MySQL DB with 3 tables as below
Table: subject_master
-----------------------
subjectId | subjectName
--------------------------------------
1 | English
2 | German
3 | French
4 | Spanish
Table: class_Master
----------------------------------
classId | className
---------------------------------------
1 | Alpha
2 | Beta
3 | Gama
4 | laser
Table: subjectAllocation
-------------------------------------------------------
allocationId | classId | subjectId
------------------------------------------------------
1 | 1(Alpha) | 1,2,3
2 | 2 (Beta) | 2,3
3 | 4 (laser) | 4,2,1
how to get list of className per subject wise
subjectName | classNames
-------------------------------------
English | Alpha, laser
German | Alpha, Beta, laser
French | Beta,Gama
Spanish | laser
I learn, GROUP_CONCAT & FIND_IN_SET but not able to use then together as below
SELECT
a.subjectName, FIND_IN_SET(c.className , (GROUP_CONCAT(b.classId))
FROM
subject_master a
LEFT JOIN subjectAllocation b ON a.subjectId = b.sectionId
LEFT JOIN class_master c ON b.classId = c.classId
GROUP BY a.subjectId
You should use FIND_IN_SET
in the ON
clause to match an element in a list, not in the SELECT
clause.
SELECT a.subjectName, GROUP_CONCAT(c.className) AS classNames
FROM subject_master AS a
LEFT JOIN subjectAllocation AS b ON FIND_IN_SET(a.subjectId, b.subjectId)
LEFT JOIN class_Master AS c ON b.classId = c.classId
GROUP BY a.subjectId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.