2013-05-30 32 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)。