MySQL Count函数将两列的结果相乘(应该分别返回每列的计数)

标签 mysql sql database join count

我有一个用户表、一个任务表和一个提醒表。我想返回每个用户的任务数和提醒数。当我只计算其中一个(提醒或任务)时,我可以让它工作,但是当我在一个查询中计算它们时,由于某种原因,它们会彼此相乘。

SQLFiddle:http://www.sqlfiddle.com/#!9/f0d6696/1/0

这是我到目前为止的查询:

SELECT
  users.name,
  COUNT(reminders.id),
  COUNT(tasks.id)
FROM users
  LEFT JOIN reminders on users.id = reminders.id
  LEFT JOIN tasks on users.id = tasks.id
GROUP BY users.id

这就是我的用户表的样子:

+---------------------------------------+
| ID | Name       | Email               |
+---------------------------------------+
| 1  | John Smith | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="046e77696d706c446169656d682a676b69" rel="noreferrer noopener nofollow">[email protected]</a>    |
| 2  | Mark Twain | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d40595a4c44436d4f4242465e034e4240" rel="noreferrer noopener nofollow">[email protected]</a>    |
| 3  | Elon Musk  | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="186b68797b7d357c6d7c7d587d75797174367b7775" rel="noreferrer noopener nofollow">[email protected]</a>|
+---------------------------------------+

这就是我的任务表的样子:

+------------------------------------------------+
| ID | Title       | Text            | Status    |
+------------------------------------------------+
| 1  | Dishes      | Kitchen = nasty | incomplete|
| 1  | Library     | drop off books  | complete  |
| 3  | Gym         | get swole dude  | incomplete|
+------------------------------------------------+

这就是我的提醒表的样子:

+------------------------------------+
| ID | Title       | Text            | 
+------------------------------------+
| 1  | Dishes      | Kitchen = nasty |
| 2  | Library     | drop off books  |
| 1  | Gym         | get swole dude  |
+------------------------------------+

我希望从上述查询中得到以下结果:

+-------------------------------------------+
| Name             | Tasks   | Reminders    |
+-------------------------------------------+
| John Smith       |    2    |      2       |
| Mark Twain       |    1    |      0       |
| Elon Musk        |    0    |      1       |
+-------------------------------------------+

我实际上得到以下信息:

+-------------------------------------------+
| Name             | Tasks   | Reminders    |
+-------------------------------------------+
| John Smith       |    4    |      4       |   <---2 tasks x 2 reminders?
| Mark Twain       |    1    |      0       |
| Elon Musk        |    0    |      1       |
+-------------------------------------------+

最佳答案

您将获得交叉连接,每项任务都有提醒。

尝试

select 
users.name,
remindercount,
taskcount
FROM users
LEFT JOIN (select id, count(*) as remindercount from reminders group by id) reminders on users.id = reminders.id
LEFT JOIN (select id, count(*) as taskcount from tasks group by id) tasks on users.id = tasks.id

关于MySQL Count函数将两列的结果相乘(应该分别返回每列的计数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888523/

相关文章:

mysql - 数据库: Making a Log of actions,如何处理各种引用?

mysql - mysql中JOIN的重复列名

c# - 创建并使用查询中的参数

sql - 如何比较 postgres 更新策略中一行的旧值和新值

mysql - MySQL 中如何汇总多个列

mysql - 如果哪里不包括主键组合的所有列,mysql超时?怎么了?

jquery - 如何在ajax调用中使用jquery中的三元运算符?

php - 在后台运行包含 MySQL 查询的 PHP 脚本而不使用 cronjobs

java - JDBC 从其中 x=(?) 选择 *

mongodb - 无法连接到 MongoDB/奇怪