[英]MySQL Count function multiplying results of two columns (Should be returning count of each column separately)
I have a table of users, a table of tasks, and a table of reminders. 我有一个用户表,一个任务表和一个提醒表。 I would like to return the count of tasks and the count of reminders per user. 我想返回任务计数和每个用户的提醒计数。 I can get it to work when I am only counting one or the other (Either reminders or tasks) but when I count both of them in one query they are for some reason multiplied by each other. 当我只计算一个或另一个(提醒或任务)时,我可以让它工作但当我在一个查询中计算它们时,由于某种原因它们相互成倍增加。
SQLFiddle: http://www.sqlfiddle.com/#!9/f0d6696/1/0 SQLFiddle: http ://www.sqlfiddle.com/#!9 / f0d6696 / 1/0
This is my query so far: 这是我目前的查询:
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
This is what my users table looks like: 这是我的用户表的样子:
+---------------------------------------+
| ID | Name | Email |
+---------------------------------------+
| 1 | John Smith | jsmith@email.com |
| 2 | Mark Twain | mtwain@books.com |
| 3 | Elon Musk | space-dude@email.com|
+---------------------------------------+
This is what my tasks table looks like: 这是我的任务表的样子:
+------------------------------------------------+
| ID | Title | Text | Status |
+------------------------------------------------+
| 1 | Dishes | Kitchen = nasty | incomplete|
| 1 | Library | drop off books | complete |
| 3 | Gym | get swole dude | incomplete|
+------------------------------------------------+
This is what my reminders table looks like: 这是我的提醒表的样子:
+------------------------------------+
| ID | Title | Text |
+------------------------------------+
| 1 | Dishes | Kitchen = nasty |
| 2 | Library | drop off books |
| 1 | Gym | get swole dude |
+------------------------------------+
I expect to get the following results from the above query: 我希望从以上查询中获得以下结果:
+-------------------------------------------+
| Name | Tasks | Reminders |
+-------------------------------------------+
| John Smith | 2 | 2 |
| Mark Twain | 1 | 0 |
| Elon Musk | 0 | 1 |
+-------------------------------------------+
I actually get the following: 我实际上得到以下内容:
+-------------------------------------------+
| Name | Tasks | Reminders |
+-------------------------------------------+
| John Smith | 4 | 4 | <---2 tasks x 2 reminders?
| Mark Twain | 1 | 0 |
| Elon Musk | 0 | 1 |
+-------------------------------------------+
Try below with distinct inside count: http://www.sqlfiddle.com/#!9/f0d6696/12 请尝试下面的内部计数: http : //www.sqlfiddle.com/#!9 / f0d6696 / 12
SELECT
users.name,
count(distinct reminders.title) as rtitle,
count(distinct tasks.title) as ttitle
FROM users
LEFT JOIN reminders on users.id = reminders.id
LEFT JOIN tasks on users.id = tasks.id
group by users.name
You are getting a cross join, every reminder for every task. 您正在获得交叉加入,每个任务的每个提醒。
try 尝试
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
Try With below query 尝试使用以下查询
SELECT id, email, name
, (SELECT COUNT(id) FROM reminders r WHERE r.id=u.id) AS reminder, (SELECT COUNT(id) FROM tasks t WHERE t.id=u.id) AS task FROM users u SELECT id,email, name
,(SELECT COUNT(id)FROM reminders r WHERE r.id = u.id)AS提醒,(SELECT COUNT(id)FROM tasks t WHERE t.id = u.id)AS task FROM users ü
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.