我有2个表,其中一个包含学生姓名和ID 学生
StdID | StdName
1 | 'John'
seconed连续包含每个主题结果
stdresults
StdID | SubjectID | SubjectName | Degree
1 | 1 | 'English' | 90
1 | 2 | 'Math' | 86
我希望结果像这样
StdID | StdName | Subject1Degree | Subject2Degree | Subject ... Degree
1 'John' 90 86 ....
无论主题数是多少
答案 0 :(得分:0)
试试这个:
SELECT T1.STDID,
T1.STDNAME,
SUM(CASE WHEN (T2.SUBJECTNAME = 'ENGLISH' AND T2.SUBJECTID = 1) THEN
T2.DEGREE ELSE 0 END) AS SUBJECT1DEGREE,
SUM(CASE WHEN (T2.SUBJECTNAME = 'MATH' AND T2.SUBJECTID = 2) THEN
T2.DEGREE ELSE 0 END)AS SUBJECT2DEGREE
FROM STUDENTS T1, STDRESULTS T2
GROUP BY T1.STDID;