I currently have the following SQL statement
MySQL Query:
SELECT
c.day,
COUNT(*)
FROM
calendar c
JOIN
visitors d
ON DAYOFMONTH(d.created) = c.day
WHERE
c.day BETWEEN DAYOFMONTH('2012-10-01') AND DAYOFMONTH('2012-10-31')
AND
site_id = 16
GROUP BY
DAYOFMONTH(d.created)
ORDER BY
DAYOFMONTH(d.created)
My Tables:
Calendar
id | day
---------
1 | 1
2 | 2
3 | 3
...
31 | 31
Visitors
id | site_id | created
-----------------------------------
1 | 16 | 2012-10-18 11:14:39
2 | 16 | 2012-10-18 11:15:17
3 | 11 | 2012-10-18 11:49:14
4 | 11 | 2012-10-18 11:49:43
5 | 16 | 2012-10-19 11:54:37
6 | 1 | 2012-10-19 05:56:31
7 | 2 | 2012-10-19 05:57:56
I have created the table, calendar as prescribed in this answer but I seem to still get the same information. I am only getting the dates where I have data.
day | COUNT(*)
---------------------
18 | 2
19 | 1
I need to also retrieve 0 on the dates that have no data.
UPDATE:
I tried this:
SELECT *
FROM calendar c
LEFT JOIN visitors d
ON DAYOFMONTH(d.created) = c.day
and
SELECT *
FROM calendar c
LEFT JOIN visitors d
ON DAYOFMONTH(d.created) = c.day
WHERE site_id = 16
I can confirm that the site_id = 16 is certainly the one killing the results.
use
LEFT JOINinstead ofINNER JOININNER JOINretrieves only rows which has atleast one match on the other table whileLEFT JOINretrieves all rows define on the lefthand side table whether it has a match or none on the other table(s).UPDATE 1
**UPDATE by Thorpe Obazee
We cannot use
COUNT(*)since it will return1every day. We also should not useDAYOFMONTHon c.day in theGROUP BYandORDER BYsince it is already what we need.