MySql查询帮助

时间:2011-07-27 14:05:45

标签: mysql sql aggregate-functions datetime-generation

我正在进行查询,以便在过去30天内从我们的数据库中获取每天的构建数量。但是,需要标记没有构建的日子。

在我的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);

1 个答案:

答案 0 :(得分: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);