MYSQL 逗号分隔 select 结果

MYSQL comma base separated select result

我有4个table,如下。

Table: Class

ClassID     |   ClassSTD
--------------------------------
1           |   STD-1<br>
2           |   STD-2

Table:部分

SectionId   |   SectionName | ClassId
--------------------------------------------
1           |   sec-A       | 1
2           |   sec-B       | 1
3           |   sec-C       | 1
4           |   sec-A       | 2
5           |   sec-B       | 2
6           |   sec-C       | 2

Table: 主题

subjectId   |   subjectName
------------------------------------
1           |   Art
2           |   Music
3           |   Play

Table SubjectAllocationToClass

    classId         |   sectionID           |   subjectId   | type
-----------------------------------------------------------------------
1(STD-1)        |   1(sec-A)            |   1(Art)      | main
1(STD-1)        |   2(sec-B)            |   1(Art)      | main
1(STD-1)        |   3(sec-C)            |   1(Art)      | optional
1(STD-1)        |   1(sec-A)            |   2(Music)    | main
1(STD-1)        |   2(sec-B)            |   2(Music)    | optional

上方 table "SubjectAllocationToClass" 显示了 class 部分的两种主题(主要和可选)的分布。 如何从 SELECT 语句中获得以下结果?

classSTD |  sectionName | Main subjectName   | Optional subjectName
-----------------------------------------------------------------------------
STD-1    |  sec-A       | Art, Music         |
STD-1    |  sec-B       | Art                |  Music
STD-1    |  sec-C       |                    |  Art

使用GROUP_CONCAT()函数:

试试这个:

SELECT D.classSTD, 
       C.sectionName, 
       GROUP_CONCAT(B.subjectName SEPARATOR ', ') AS subjectName
FROM SubjectAllocationToClass A 
INNER JOIN Subject B ON A.subjectId = B.SubjectId 
INNER JOIN Section C ON A.sectionId = C.SectionId   
INNER JOIN Class D ON A.classID = D.ClassID 
GROUP BY D.ClassID, C.SectionId;

关于你的另一个问题

SELECT D.classSTD, 
       C.sectionName, 
       GROUP_CONCAT(B.subjectName SEPARATOR ', ') AS subjectName, 
       GROUP_CONCAT(CASE WHEN B.type = 'main' THEN B.subjectName ELSE NULL END SEPARATOR ', ') AS mainsubjectName, 
       GROUP_CONCAT(CASE WHEN B.type = 'optional' THEN B.subjectName ELSE NULL END SEPARATOR ', ') AS optionalSubjectName
FROM SubjectAllocationToClass A 
INNER JOIN SUBJECT B ON A.subjectId = B.SubjectId 
INNER JOIN Section C ON A.sectionId = C.SectionId   
INNER JOIN Class D ON A.classID = D.ClassID 
GROUP BY D.ClassID, C.SectionId;

您可以通过 group_concat 这样做:

select classSTD , SectionName ,  group_concat(`subjectName` separator ',') as `subjectName` 
FROM SubjectAllocationToClass 
INNER JOIN Class ON SubjectAllocationToClass.classId = Class.ClassID 
INNER JOIN Section ON SubjectAllocationToClass.sectionID   = Section.sectionId
INNER JOIN Subject ON SubjectAllocationToClass.subjectID   = Subject .subjectId  
GROUP BY  SubjectAllocationToClass.subjectID ;

使用GROUP_CONCAT

 SELECT  sec.SectionName , cl.classSTD, GROUP_CONCAT(`sub.subjectName`) as `subjectName` FROM SubjectAllocationToClass AS al JOIN Class AS cl ON al.classId = cl.ClassID JOIN Section AS sec ON al.sectionID  = sec.sectionId JOIN Subject AS sub ON al.subjectID   = sub.subjectId  
GROUP BY  al.subjectID;