输出MySQL结果

时间:2013-05-30 13:18:08

标签: mysql

所以我有3张桌子:

student
-studentid
-studentname

course_offerings
-course_offeringid
-course
-type

scores
-student_studentid
-course_offering_course_offeringid
-score

当我查询时:

SELECT studentid, studentname, course, type, score
FROM scores
INNER JOIN student ON scores.student_studentid = student.studentid
INNER JOIN course_offering ON scores.course_offering_course_offeringid = course_offering.course_offeringid

我得到的输出类似于:

studentid   studentname     course  type    score
123345      Doe, John       123     Exam 1  100
123345      Doe, John       123     Exam 2  95
123345      Doe, John       123     Exam 3  75
123345      Doe, John       123     Final   93
543211      Doe, Jane       123     Exam 1  70
543211      Doe, Jane       123     Exam 2  91
543211      Doe, Jane       123     Exam 3  99
543211      Doe, Jane       123     Final   43
.
.
.

我想要的输出是:

studentid   studentname     course  Exam 1  Exam 2  Exam 3  Final
123345      Doe, John       123     100     95       75     93
543211      Doe, Jane       123     70      91      99      43

这只能用MySQL吗?

2 个答案:

答案 0 :(得分:0)

不是没有硬编码考试的数量。您可以带学生,在考试中加入他们3次,确保first id < secondid < thirdid停止双打。

如果您需要额外的考试,或者某人只有2门考试,您需要处理它。如果不对3(或更多/更少)连接进行硬编码,则无法进行此类转换

答案 1 :(得分:0)

好吧,SQL不允许你这样做,所以在你的应用程序中执行这个处理可能会更好。

如果确实希望在MySQL中执行此操作,则可以使用比纯SQL稍强一些的stored routines(例如,您可以使用FOR-loop)。< / p>

相关问题