汇总 MySQL 中两个表之间的列
Sum up columns between two tables in MySQL
我有一个应用程序,学生可以从列表中选择第 1、第 2 和第 3 个选项。
表 1 是他们可以选择的列表:
id
day
1
Monday
2
Thusday
3
Wednesday
表2是他们填写的选项:
id
first_choice
second_choice
third_choice
12345
1
3
2
23456
3
2
1
34567
2
1
3
45678
1
2
3
我正在努力的是我想计算每天的选择和优先级以获得这样的列表:
id
first_choice
second_choice
third_choice
Monday
2
1
1
Thusday
1
2
1
Wednesday
1
1
2
SELECT a.day, count(b.first_choice), count(c.second_choice), count(d.third_choice) FROM table1 a LEFT JOIN table2 b ON a.id = b.first_choice LEFT JOIN table2 c ON a.id = c.second_choice LEFT JOIN table2 d ON a.id = d.third_choice GROUP BY a.day
但是,这样做我最终得到了这个
id
first_choice
second_choice
third_choice
Monday
2
2
2
Thusday
2
2
2
Wednesday
2
2
2
谁能帮我查询一下?
提前致谢
在那些table结构中,我通常使用子查询而不是连接。
SELECT
a.day,
(SELECT COUNT(*) FROM Table2 WHERE first_choice = a.id) AS first_choice,
(SELECT COUNT(*) FROM Table2 WHERE second_choice = a.id) AS second_choice,
(SELECT COUNT(*) FROM Table2 WHERE third_choice = a.id) AS third_choice
FROM Table1 a
您可以使用单个 INNER JOIN
并使用条件 IF
语句计算:
SELECT
Table1.id,
Table1.day,
COUNT(IF(Table2.first_choice=Table1.id, Table2.first_choice, NULL)) AS first_choice,
COUNT(IF(Table2.second_choice=Table1.id, Table2.second_choice, NULL)) AS second_choice,
COUNT(IF(Table2.third_choice=Table1.id, Table2.third_choice, NULL)) AS third_choice
FROM
Table1
INNER JOIN
Table2
ON
Table1.id = Table2.first_choice
OR
Table1.id = Table2.second_choice
OR
Table1.id = Table2.third_choice
GROUP BY
Table1.id,
Table1.day
参考这个fiddle:https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eea27fe9364c0fe4b96a846c9441f723
我有一个应用程序,学生可以从列表中选择第 1、第 2 和第 3 个选项。
表 1 是他们可以选择的列表:
id | day |
---|---|
1 | Monday |
2 | Thusday |
3 | Wednesday |
表2是他们填写的选项:
id | first_choice | second_choice | third_choice |
---|---|---|---|
12345 | 1 | 3 | 2 |
23456 | 3 | 2 | 1 |
34567 | 2 | 1 | 3 |
45678 | 1 | 2 | 3 |
我正在努力的是我想计算每天的选择和优先级以获得这样的列表:
id | first_choice | second_choice | third_choice |
---|---|---|---|
Monday | 2 | 1 | 1 |
Thusday | 1 | 2 | 1 |
Wednesday | 1 | 1 | 2 |
SELECT a.day, count(b.first_choice), count(c.second_choice), count(d.third_choice) FROM table1 a LEFT JOIN table2 b ON a.id = b.first_choice LEFT JOIN table2 c ON a.id = c.second_choice LEFT JOIN table2 d ON a.id = d.third_choice GROUP BY a.day
但是,这样做我最终得到了这个
id | first_choice | second_choice | third_choice |
---|---|---|---|
Monday | 2 | 2 | 2 |
Thusday | 2 | 2 | 2 |
Wednesday | 2 | 2 | 2 |
谁能帮我查询一下? 提前致谢
在那些table结构中,我通常使用子查询而不是连接。
SELECT
a.day,
(SELECT COUNT(*) FROM Table2 WHERE first_choice = a.id) AS first_choice,
(SELECT COUNT(*) FROM Table2 WHERE second_choice = a.id) AS second_choice,
(SELECT COUNT(*) FROM Table2 WHERE third_choice = a.id) AS third_choice
FROM Table1 a
您可以使用单个 INNER JOIN
并使用条件 IF
语句计算:
SELECT
Table1.id,
Table1.day,
COUNT(IF(Table2.first_choice=Table1.id, Table2.first_choice, NULL)) AS first_choice,
COUNT(IF(Table2.second_choice=Table1.id, Table2.second_choice, NULL)) AS second_choice,
COUNT(IF(Table2.third_choice=Table1.id, Table2.third_choice, NULL)) AS third_choice
FROM
Table1
INNER JOIN
Table2
ON
Table1.id = Table2.first_choice
OR
Table1.id = Table2.second_choice
OR
Table1.id = Table2.third_choice
GROUP BY
Table1.id,
Table1.day
参考这个fiddle:https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eea27fe9364c0fe4b96a846c9441f723