小编典典

MySql查询:包括具有COUNT(id)== 0但仅在最近30天内的天

sql

我正在查询以获取最近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);

阅读 230

收藏
2021-04-28

共1个答案

小编典典

您需要一个日期表,然后左联接到builds表。

像这样的东西:

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);
2021-04-28