0

MySQL 查询:

SELECT  c.day,
        COUNT(site_id)
FROM    calendar c
        LEFT JOIN
        (
            SELECT *
            FROM   visitors
            WHERE  site_id = 16
        ) d ON  DAYOFMONTH(d.created) = c.day
WHERE c.day BETWEEN DAYOFMONTH('2012-10-01') AND DAYOFMONTH('2012-10-31')
GROUP BY c.day
ORDER BY c.day

我的桌子

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(*)
-------------
1   |       0
2   |       0
3   |       0
....
18  |       2
19  |       1
...
31  |       0

虽然,现在,我在检索 UNIX_TIMESTAMP 时遇到了问题day,我需要用于绘图目的。

如何从c.day查询中检索它?

4

1 回答 1

0

编辑:

SELECT
  UNIX_TIMESTAMP('2012-10-01' + INTERVAL c.day - 1 DAY) unix_ts_day,
  COUNT(v.site_id)
FROM
  calendar c
LEFT JOIN (
  SELECT * FROM visitors
    WHERE site_id = 16 AND DATE(created) BETWEEN '2012-10-01' AND '2012-10-31'
  ) v
  ON DAYOFMONTH(v.created) = c.day
GROUP BY
  unix_ts_day
于 2012-10-22T06:05:09.660 回答