对 MySQL 中两个表之间的列求和

发布于 2025-01-20 03:26:11 字数 3346 浏览 0 评论 0原文

我有一个应用程序,学生可以从列表中选择第一、第二和第三三个选项。

表 1 是他们可以选择的列表:

idday
1星期一
2星期四
3星期三

表 2 是他们填写的选择:

idfirst_choicesecondary_choicethird_choice12345
23456132
3321
34567211
4567823挣扎

我正在 是我想计算每天的选择和优先级以获得像这样的列表这个:

idfirst_choicesecondary_choicethird_choice2
Monday211
Today121
Wednesday112
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 

,我最终得到这个

idfirst_choicesecondary_choicethird_choice但是,通过这样做
Monday222
Today222
Wednesday22

谁能帮 询问? 提前致谢

I have an application where students do three choices 1st, 2nd and 3rd from a list.

Table1 is the list they can choose from:

idday
1Monday
2Thusday
3Wednesday

Table2 is they fill in their choices:

idfirst_choicesecond_choicethird_choice
12345132
23456321
34567213
45678123

What I'm struggling with is that I want to count choices per day and priority to get a list like this:

idfirst_choicesecond_choicethird_choice
Monday211
Thusday121
Wednesday112
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 

But, by doing so I end up with this

idfirst_choicesecond_choicethird_choice
Monday222
Thusday222
Wednesday222

Could anyone help me with the query?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2025-01-27 03:26:11

在这些表结构中,我通常使用子查询而不是加入。


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

In those table structures, I normally use subquery instead of join.


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

那小子欠揍 2025-01-27 03:26:11

您可以使用单个 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

请参阅此小提琴:https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eea27fe9364c0fe4b96a846c9441f723

You can use a single INNER JOIN and work it out with conditional IF statements:

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

Refer to this fiddle: https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=eea27fe9364c0fe4b96a846c9441f723

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文