计算多次连接的表中的值

发布于 2024-12-07 05:35:48 字数 1367 浏览 0 评论 0原文

我在计算多次连接的表时遇到问题。

问题表:

+----+----------+
| id | question |
+----+----------+
|  1 | Foo?     |
+----+----------+

答案表:

+----+-------------+--------+
| id | question_id | choice |
+----+-------------+--------+
|  1 |           1 |      1 |
|  2 |           1 |      1 |
|  3 |           1 |      1 |
|  4 |           1 |      2 |
|  5 |           1 |      3 |
|  6 |           1 |      3 |
+----+-------------+--------+

预期结果:(

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     3 |     1 |     2 |
+----------+-------+-------+-------+

失败的)查询及其结果:

SELECT
    q.question AS question,
    COUNT(a1.id) AS num_1,
    COUNT(a2.id) AS num_2,
    COUNT(a3.id) AS num_3
FROM
    question q
    LEFT JOIN answer a1 ON a1.question_id = q.id AND a1.choice = 1
    LEFT JOIN answer a2 ON a2.question_id = q.id AND a2.choice = 2
    LEFT JOIN answer a3 ON a3.question_id = q.id AND a3.choice = 3
GROUP BY
        q.id

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     6 |     6 |     6 |
+----------+-------+-------+-------+

我不明白为什么会得到这个结果。 你能帮助我吗?

I have a problem counting a table joined several times.

The question table :

+----+----------+
| id | question |
+----+----------+
|  1 | Foo?     |
+----+----------+

The answer one :

+----+-------------+--------+
| id | question_id | choice |
+----+-------------+--------+
|  1 |           1 |      1 |
|  2 |           1 |      1 |
|  3 |           1 |      1 |
|  4 |           1 |      2 |
|  5 |           1 |      3 |
|  6 |           1 |      3 |
+----+-------------+--------+

The expected result :

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     3 |     1 |     2 |
+----------+-------+-------+-------+

The (failing) query and its result :

SELECT
    q.question AS question,
    COUNT(a1.id) AS num_1,
    COUNT(a2.id) AS num_2,
    COUNT(a3.id) AS num_3
FROM
    question q
    LEFT JOIN answer a1 ON a1.question_id = q.id AND a1.choice = 1
    LEFT JOIN answer a2 ON a2.question_id = q.id AND a2.choice = 2
    LEFT JOIN answer a3 ON a3.question_id = q.id AND a3.choice = 3
GROUP BY
        q.id

+----------+-------+-------+-------+
| question | num_1 | num_2 | num_3 |
+----------+-------+-------+-------+
| Foo?     |     6 |     6 |     6 |
+----------+-------+-------+-------+

I don't understand why I get this result.
Can you help me?

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

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

发布评论

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

评论(2

哑剧 2024-12-14 05:35:48

因为 choice = 1 给出 3 行,choice = 2 给出 1 行,choice = 3 给出 2 行,1 * 2 * 3 = 6。如果您删除group by并聚合并查看结果,那么结果应该很清楚。您可以使用

SELECT
    q.question AS question,
    COUNT(CASE WHEN a.choice = 1 THEN 1 END) AS num_1,
    COUNT(CASE WHEN a.choice = 2 THEN 1 END) AS num_2,
    COUNT(CASE WHEN a.choice = 3 THEN 1 END) AS num_3
FROM
    question q
    LEFT JOIN answer a ON a.question_id = q.id AND a.choice IN (1,2,3)
GROUP BY
        q.id,
        q.question

Because choice = 1 gives 3 rows, choice = 2 gives 1 row, choice = 3 gives 2 rows and 1 * 2 * 3 = 6. if you remove the group by and aggregates and look at the results it should be clear. You can use

SELECT
    q.question AS question,
    COUNT(CASE WHEN a.choice = 1 THEN 1 END) AS num_1,
    COUNT(CASE WHEN a.choice = 2 THEN 1 END) AS num_2,
    COUNT(CASE WHEN a.choice = 3 THEN 1 END) AS num_3
FROM
    question q
    LEFT JOIN answer a ON a.question_id = q.id AND a.choice IN (1,2,3)
GROUP BY
        q.id,
        q.question
无人接听 2024-12-14 05:35:48

如果您在没有计数和分组的情况下运行查询,您将看到得到如下结果:

+------+------+------+------+
| q    | num1 | num2 | num3 |
+------+------+------+------+
| foo  |    1 |    4 |    5 |
| foo  |    1 |    4 |    6 |
| foo  |    2 |    4 |    5 |
| foo  |    2 |    4 |    6 |
| foo  |    3 |    4 |    5 |
| foo  |    3 |    4 |    6 |
+------+------+------+------+

正如预期的那样,有 6 行,因此每个别名字段将为您提供 6 的计数。Martin Smith 在上面得到了正确的答案。

If you run your query without the counts and grouping, you'll see you get results like this:

+------+------+------+------+
| q    | num1 | num2 | num3 |
+------+------+------+------+
| foo  |    1 |    4 |    5 |
| foo  |    1 |    4 |    6 |
| foo  |    2 |    4 |    5 |
| foo  |    2 |    4 |    6 |
| foo  |    3 |    4 |    5 |
| foo  |    3 |    4 |    6 |
+------+------+------+------+

As expected, 6 rows, so each aliased field will give you a count of 6. Martin Smith's got the right answer above.

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