2012-10-22 60 views
0

MySQL查詢:獲得UNIX時間與數量

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 

我用上面的查詢檢索瀏覽網站的每日結果。查詢解決了我的question here

結果:

day | COUNT(*) 
------------- 
1 |  0 
2 |  0 
3 |  0 
.... 
18 |  2 
19 |  1 
... 
31 |  0 

雖然,現在,我有從day我需要繪圖的目的檢索UNIX_TIMESTAMP問題。

如何從查詢中的c.day中檢索它?

回答

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 
+0

v.created是大多數天NULL。例如,v.created在第一行將爲NULL。 '1 | 0 | NULL「,因爲它是一個LEFT JOIN。 –

+0

我需要得到本月的所有日子。從第一天開始到最後一天。這意味着我每次需要31行。 –

+0

我改變了答案。 – Devart