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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.