MYSQL逗号基分隔选择结果

时间:2015-12-28 12:29:32

标签: mysql sql select group-by group-concat

我有4张桌子,如下所示。

表:

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

表:部分

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

表:主题

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

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

上表“SubjectAllocationToClass”显示了两种类型主题(主要和可选)在课程部分的分布。 如何从SELECT语句中获得以下结果?

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

3 个答案:

答案 0 :(得分:1)

使用 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;

答案 1 :(得分:1)

你可以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 ;

答案 2 :(得分:0)

USE 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;