我目前有以下 SQL 语句
MySQL 查询:
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)
我的表:
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
我已经按照这个答案的规定创建了表格和日历,但我似乎仍然得到了相同的信息。我只得到我有数据的日期。
day | COUNT(*)
---------------------
18 | 2
19 | 1
我还需要检索0
没有数据的日期。
更新:
I tried this:
SELECT *
FROM calendar c
LEFT JOIN visitors d
ON DAYOFMONTH(d.created) = c.day
和
SELECT *
FROM calendar c
LEFT JOIN visitors d
ON DAYOFMONTH(d.created) = c.day
WHERE site_id = 16
我可以确认那site_id = 16
肯定是杀死结果的那个。