2013-05-30 30 views
0

所以我有3个表:1.4.3 MySQL的结果

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可能吗?

+0

我认为它可能..我可以问,如果这是你有完整的表结构?我认为你在course_offering和成绩表上都缺少studentid。 – mCube

+0

我认为在'scores'中有一个studentid,没有理由在'offerings'表中找到它。 – kirelagin

+0

是的,我相信这是正确的。对于那个很抱歉。我还建议您删除“类型”字段并将其放在“分数”表上。 – mCube

回答

0

不是没有硬编码的考试量。你可以带走学生,在考试中加入3次不同的时间,确保first id < secondid < thirdid停止双打发生。

如果您需要额外的考试,或者某人只有2次考试,则需要处理。如果没有对3个(或更多/更少)连接进行硬编码,这种关键是不可能的。

0

那么,SQL不会让你这么做,所以最好在你的应用程序中执行这个处理。

如果你真的想这样做在MySQL中,你可以使用stored routines这是稍微比普通SQL更强大(例如,你可以使用一个FOR-loop)。