Soldato
Hi All,
I've been rattling my brains for most of the day and can't seem to figure out why a solution to my issue.
Essentially i have two tables, one called 'users' -
and another called, 'visits' -
And what i'm wanting is the weeks totals (include no results, ie - zero) to be displayed against the 'users' table like so -
Current query is -
However because of the date range condition, only users with positive totals (> 0) are being displayed or nothing is displayed if there are no visits within that date range; removing the condition displays all of the users with their 'weekly_totals', include 'zero' totals, but obviously i need the date condition.
I don't doubt it's something obvious which i'm not seeing but is there a way to force the output of all the users table with zero 'weekly_total` regardless of there being data in the 'LEFT' table due to the date condition?
Much appreciated for any help or pointers!
I've been rattling my brains for most of the day and can't seem to figure out why a solution to my issue.
Essentially i have two tables, one called 'users' -
Code:
+----------+------------+
| USERS_ID | USERS_NAME |
+----------+------------+
| | |
| 1 | Dave |
| | |
| 2 | Bob |
| | |
| 3 | Jack |
+----------+------------+
and another called, 'visits' -
Code:
+----------+----------+------------+
| VISIT_ID | USERS_ID | VISIT_DATE |
+----------+----------+------------+
| | | |
| 1 | 3 | 2015-11-23 |
| | | |
| 2 | 1 | 2015-11-24 |
| | | |
| 3 | 3 | 2015-11-25 |
| | | |
| 4 | 3 | 2015-11-24 |
| | | |
| 5 | 1 | 2015-11-26 |
| | | |
| 6 | 3 | 2015-11-27 |
+----------+----------+------------+
And what i'm wanting is the weeks totals (include no results, ie - zero) to be displayed against the 'users' table like so -
Code:
+-----------+-------+
| USERS_NAME | TOTAL |
+-----------+-------+
| | |
| Dave | 2 |
| | |
| Bob | 0 |
| | |
| Jack | 4 |
+-----------+-------+
Current query is -
Code:
SELECT `users`.`users_name`, COUNT(`visits`.`users_id`) AS `weekly_total` FROM `users` LEFT JOIN `visits` ON `users`.`users_id` = `visits`.`users_id` WHERE (`visits`.`visit_date` >= '2015-11-23' AND `visits`.`visit_date` <= '2015-11-27') GROUP BY `visits`.`users_id`, `users`.`users_id`
However because of the date range condition, only users with positive totals (> 0) are being displayed or nothing is displayed if there are no visits within that date range; removing the condition displays all of the users with their 'weekly_totals', include 'zero' totals, but obviously i need the date condition.
I don't doubt it's something obvious which i'm not seeing but is there a way to force the output of all the users table with zero 'weekly_total` regardless of there being data in the 'LEFT' table due to the date condition?
Much appreciated for any help or pointers!