簡體   English   中英

如何編寫這個復雜的SQL語句

[英]How to write this complex SQL statement

我的MySQL表中有三個相同的表

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    |

現在,我要實現的是按學生姓名third_term_result.mark每個WHERE subject_id = 7組學生的first_term_result.mark second_term_result.markthird_term_result.markSUM()

另一個非常重要的問題是,我將為first_term + second_term + third_term計算每個學生的總和,並且還希望能夠為該學生和DESC中的科目訂購總和,因此我可以據此對他們進行定位在php上更輕松,請讓我知道。

謝謝

這對我來說似乎很復雜,但是我知道這里有一些專家可以做到這一點,我讀過某個地方,即使使用匯總功能也可以訂購。

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

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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM