簡體   English   中英

與mysql語句混淆,三張表

[英]Confused with mysql statement, three tables

所以我有從3個不同的表作為矩陣打印出表數據的問題。 搜索互聯網后,我狂熱地找到了解決我問題的方法。 但我不知道,該mysql語句如何工作。 這是我想做的一個例子

我想我找到了一種解決方案,所以如果有人可以幫助我解釋一下它如何工作?

SELECT names.codename,
s1.score AS "Score1", s1.comment AS "Comments1",
s2.score AS "Score2", s2.comment AS "Comments2",
SUM(st.score) AS "Total"
FROM students names 
LEFT JOIN scores s1 ON s1.act_id=1 AND names.id=s1.student_id 
LEFT JOIN scores s2 ON s2.act_id=2 AND names.id=s2.student_id 
LEFT JOIN scores st ON names.id=st.student_id
WHERE names.codename <> ''
GROUP BY names.codename
ORDER BY names.codename;

students table:
+----+---------------+
| id | codename      |
+----+---------------+
|  1 | Budy          |
+----+---------------+

assignments table:
+--------+------------+
| act_id | name       |
+--------+------------+
|      1 | Activity 1 |
|      2 | Activity 2 |
+--------+------------+

scores table:
+------------+--------+-------+
| student_id | act_id | score |
+------------+--------+-------+
|          1 |      1 |    10 |
|          1 |      2 |    10 |
+------------+--------+-------+

現在的問題是,我想在頂部列出作業,並在名稱旁邊列出分數。 像這樣:

+---------------+------------+------------+-------+
| codename      | Activity 1 | Activity 2 | Total |
+---------------+------------+------------+-------+
|          budy |         10 |         10 |    20 |
+---------------+------------+------------+-------+

嘗試這個

SELECT s.codename, SUM(IF(act.act_id=1, act.score, 0)) AS 'Activity 1', SUM(IF(act.act_id=2, act.score, 0)) AS 'Activity 2', total.score AS 'total' 
FROM
    students s
    INNER JOIN (SELECT student_id, act_id, SUM(score) AS score FROM scores GROUP BY 1, 2) act ON s.id=act.student_id
    INNER JOIN (SELECT student_id, SUM(score) AS score FROM scores GROUP BY 1) total ON s.id=total.student_id
GROUP BY 1

這將僅返回在scores表中有記錄的studentsscores 如果要列出所有學生,則必須將INNER JOIN更改為LEFT OUTER JOIN

更新

我已經更新了查詢並刪除了第一個子查詢

SELECT student_id, act_id, SUM(score) AS score FROM scores GROUP BY 1, 2

如果student_idact_id是復合鍵,則act_id ,因此我們可以直接使用scores表。 這樣查詢變成

SELECT s.codename, SUM(IF(act.act_id=1, act.score, 0)) AS 'Activity 1', SUM(IF(act.act_id=2, act.score, 0)) AS 'Activity 2', total.score AS 'total' 
FROM
    students s
    INNER JOIN scores as 'act' ON s.id=act.student_id
    INNER JOIN (SELECT student_id, SUM(score) AS score FROM scores GROUP BY 1) total ON s.id=total.student_id
GROUP BY 1

說明

studentscores之間的JOIN獲取所有student所有分數(我添加了另一個ID為2且名稱為Pal學生),例如

+----+----------+------------+--------+-------+
| id | codename | student_id | act_id | score |
+----+----------+------------+--------+-------+
|  1 | Buddy    |          1 |      1 |    10 |
|  1 | Buddy    |          1 |      2 |    10 |
|  2 | Pal      |          2 |      1 |    15 |
|  2 | Pal      |          2 |      2 |    10 |
+----+----------+------------+--------+-------+

子查詢

SELECT student_id, SUM(score) AS score FROM scores GROUP BY 1

像這樣為每個學生獲取分數總和

+------------+-------+
| student_id | score |
+------------+-------+
|          1 |    20 |
|          2 |    25 |
+------------+-------+

然后我們使用以下命令將結果放到主要的SELECT部分中

SELECT s.codename, SUM(IF(act.act_id=1, act.score, 0)) AS 'Activity 1', SUM(IF(act.act_id=2, act.score, 0)) AS 'Activity 2', total.score AS 'total'...

如果act_id=1在“活動1”列中添加得分,類似地,如果act_id=2我們在“活動2”列中添加得分,最后我們使用子查詢的總和作為“總計”

我希望這可以澄清您的問題

暫無
暫無

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

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