2011-07-27 160 views
4

我在查詢過去30天內從我們的數據庫中獲取每天的構建數量。但是需要標記那些沒有構建的日子。MySql查詢幫助

在我的WHERE子句中,我使用submittime來確定是否有構建,我如何修改這個以包含COUNT(id)== 0但只在最近30天內的日子。

原始查詢:

SELECT COUNT(id) AS 'Past-Month-Builds', 
      CONCAT(MONTH(submittime), '-', DAY(submittime)) as 'Month-Day' 
    FROM builds 
    WHERE DATE(submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day) 
GROUP BY MONTH(submittime), DAY(submittime); 

我已經試過:

SELECT COUNT(id) AS 'Past-Month-Builds', 
      CONCAT(MONTH(submittime), '-', DAY(submittime)) as 'Month-Day' 
    FROM builds 
    WHERE DATE(submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day) 
     OR COUNT(id) = 0 
GROUP BY MONTH(submittime), DAY(submittime); 
+2

聽起來就像是問題是,你沒有的日期在'builds'表 - 看看其他'的datetime-generation'標記爲問題的例子。 –

回答

1

您需要的日期表,然後離開加入到建立表。

事情是這樣的:

SELECT 
    COUNT(id) AS 'Past-Month-Builds', 
    CONCAT(MONTH(DateTable.Date), '-', DAY(DateTable.Date)) as 'Month-Day' 
FROM DateTable 
    LEFT JOIN builds ON DATE(builds.submittime) = DateTable.Date 
WHERE DateTable.Date >= DATE_SUB(CURDATE(), INTERVAL 30 day) 
GROUP BY MONTH(submittime), DAY(submittime); 
+0

謝謝,這工作很好! –