2013-10-18 27 views
1

我有我的MySQL表即如何寫這個複雜的SQL語句

first_term_result,second_term_result三個相同的表和third_term_result

這是列在它

exam_type_id | student_id | subject_id | mark | 

或示例帶有虛擬數據

注意:每個科目有三種不同的考試類型(CA1,CA2,CA3和考試), 有三個這樣的表,它們具有相同的東西但數據不同,因爲它包含第一學期的數據,第二學期的數據和第三學期的最後一個數據。

first_term_result: 

exam_type_id | student_id | subject_id | mark | 
    1  | 6  |  7  | 12 | 
    2  | 6  |  7  | 9  | 
    3  | 6  |  7  | 13 | 
    4  | 6  |  7  | 45 | 
    1  | 4  |  7  | 7  | 
    2  | 4  |  7  | 5  | 
    3  | 4  |  7  | 10 | 
    4  | 4  |  7  | 34 | 

second_term_result: 

exam_type_id | student_id | subject_id | mark | 
    1  | 6  |  7  | 15 | 
    2  | 6  |  7  | 6  | 
    3  | 6  |  7  | 10 | 
    4  | 6  |  7  | 50 | 
    1  | 4  |  7  | 6  | 
    2  | 4  |  7  | 3  | 
    3  | 4  |  7  | 9  | 
    4  | 4  |  7  | 44 | 


third_term_result: 

exam_type_id | student_id | subject_id | mark | 
    1  | 6  |  7  | 17 | 
    2  | 6  |  7  | 8  | 
    3  | 6  |  7  | 15 | 
    4  | 6  |  7  | 67 | 
    1  | 4  |  7  | 12 | 
    2  | 4  |  7  | 8  | 
    3  | 4  |  7  | 12 | 
    4  | 4  |  7  | 50 | 

現在我想達到什麼是學生的名字讓每個學生WHERE subject_id = 7組的SUM()first_term_result.marksecond_term_result.markthird_term_result.mark

另一個非常重要的問題是,我將計算每個學生的first_term + second_term + third_term的總和,並且還希望能夠爲該學生和DESC中的主題訂購總計,以便我可以相應地定位它們請如果它會更容易在PHP請讓我知道。

感謝

在我看來很複雜,但我知道這裏有大師誰CA做到這一點,我讀的地方,可以通過甚至被用來彙總時訂購。

下面是我的代碼,顯然不工作。

SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS sname, 
SUM(f.mark) AS first_total, 
SUM(se.mark) AS second_total, 
SUM(t.mark) AS third_total 
SUM(f.first_total,second.total,third_total) as GT // just to show my intention 
FROM students s, first_term_result f, second_term_result se, third_term_result t 
WHERE s.studentID=f.student_id AND 
s.studentID=se.student_id AND 
s.studentID=t.student_id AND 
f.subject_id=7 AND 
se.subject_id=7 AND 
t.subject_id=7 
GROUP BY sname ORDER BY GT DESC 
+7

這將是迄今爲止那麼複雜,如果你進一步規範你的數據。爲術語製作一列,並將所有術語保留在同一張表格中。 –

+0

@DMac認真這使我愚蠢,似乎可以知道如何我可以實現相同,如果我將所有條款保留在同一張表 – user2666633

+0

*複雜*:D我們使用多達200行的查詢.. – DanFromGermany

回答

1
SELECT CONCAT(MS.fname,' ',MS.mname,' ',MS.lname) AS sname, 
    M.FTotal, M.STotal, M.TTotal, 
    (M.FTotal + M.STotal + M.TTotal) AS GrandTotal 
FROM (
    SELECT st.studentID, 
    (
     SELECT SUM(f.mark) 
     FROM first_term_result AS f 
     WHERE f.subject_id = 7 
     AND f.student_id = st.studentID 
    ) AS FTotal, 
    (
     SELECT SUM(s.mark) 
     FROM second_term_result AS s 
     WHERE s.subject_id = 7 
     AND s.student_id = st.studentID 
    ) AS STotal, 
    (
     SELECT SUM(t.mark) 
     FROM third_term_result AS t 
     WHERE t.subject_id = 7 
     AND t.student_id = st.studentID 
    ) AS TTotal 
    FROM students AS st 
    WHERE st.studentID IN (
    SELECT studentID 
    FROM first_term_result AS fs 
    WHERE fs.subject_id = 7 
    AND fs.student_id = st.studentID) 
    GROUP BY st.studentID 
) AS M 
JOIN students MS ON M.studentID = MS.studentID 
ORDER BY (M.FTotal + M.STotal + M.TTotal) DESC 
+0

這運行良好,但給了我錯誤的計算數據 – user2666633

+0

我已經更新了我的答案,我創建了子查詢來計算每個學期的總計。 OMG! – Linger

+0

OMG!這個作品,但列出學生表中的所有學生的名字沒有結果表上的標記我認爲左結合表上的JOIN可以修復它,將欣賞先生 – user2666633