MySQL select 按月 returns 所有月份均为 NULL

MySQL select by month returns NULL for all months

使用 MySQL 5.6 我正在尝试 select 过去 12 个月中每个 month 的总 inspections,如果有 none。我似乎在这里遗漏了一些东西,因为输出全部是 NULLdate_inspected 字段只是一个常规的 SQL date

根据我的理解,结构应该是 [conditional statement, output variable, default value] 但即使是 0 也会被忽略以支持 NULL。我想了解我在这里做错了什么。

Table:

Column          Type        Null    
id              int(11)     No           
inspector_id    int(11)     Yes          
company_id      int(11)     Yes      
date_inspected  date        No           
start_time      datetime    No           
end_time        datetime    No       

查询:

SELECT
  SUM(IF(MONTH = 'Jan', total, 0)) AS 'Januari',
  SUM(IF(MONTH = 'Feb', total, 0)) AS 'Februari',
  SUM(IF(MONTH = 'Mar', total, 0)) AS 'Maart',
  SUM(IF(MONTH = 'Apr', total, 0)) AS 'April',
  SUM(IF(MONTH = 'May', total, 0)) AS 'Mei',
  SUM(IF(MONTH = 'Jun', total, 0)) AS 'Juni',
  SUM(IF(MONTH = 'Jul', total, 0)) AS 'Juli',
  SUM(IF(MONTH = 'Aug', total, 0)) AS 'Augustus',
  SUM(IF(MONTH = 'Sep', total, 0)) AS 'September',
  SUM(IF(MONTH = 'Oct', total, 0)) AS 'Oktober',
  SUM(IF(MONTH = 'Nov', total, 0)) AS 'November',
  SUM(IF(MONTH = 'Dec', total, 0)) AS 'December',
  SUM(total) AS all_months
  FROM (
        SELECT MONTH(date_inspected) AS MONTH, COUNT(*) AS total
        FROM inspection
        WHERE date_inspected BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)
        GROUP BY MONTH
  ) AS SubTable

输出

{ ["Januari"]=> NULL 
["Februari"]=> NULL 
["Maart"]=> NULL 
["April"]=> NULL 
["Mei"]=> NULL 
["Juni"]=> NULL 
["Juli"]=> NULL 
["Augustus"]=> NULL 
["September"]=> NULL 
["Oktober"]=> NULL 
["November"]=> NULL 
["December"]=> NULL 
["all_months"]=> NULL }

更新:

Gordon Linoff

的解决方案
SELECT
     SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END) AS 'Januari',
     SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END) AS 'Februari',
     SUM(CASE WHEN MONTH(date_inspected) = 3 THEN 1 ELSE 0 END) AS 'Maart',
     SUM(CASE WHEN MONTH(date_inspected) = 4 THEN 1 ELSE 0 END) AS 'April',
     SUM(CASE WHEN MONTH(date_inspected) = 5 THEN 1 ELSE 0 END) AS 'Mei',
     SUM(CASE WHEN MONTH(date_inspected) = 6 THEN 1 ELSE 0 END) AS 'Juni',
     SUM(CASE WHEN MONTH(date_inspected) = 7 THEN 1 ELSE 0 END) AS 'Juli',
     SUM(CASE WHEN MONTH(date_inspected) = 8 THEN 1 ELSE 0 END) AS 'Augustus',
     SUM(CASE WHEN MONTH(date_inspected) = 9 THEN 1 ELSE 0 END) AS 'September',
     SUM(CASE WHEN MONTH(date_inspected) = 10 THEN 1 ELSE 0 END) AS 'Oktober',
     SUM(CASE WHEN MONTH(date_inspected) = 11 THEN 1 ELSE 0 END) AS 'November',
     SUM(CASE WHEN MONTH(date_inspected) = 12 THEN 1 ELSE 0 END) AS 'December'
FROM inspection
WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND  NOW()

据我了解,我们已经为 SUM() 提供了一个条件 CASE 语句,如果当前记录的 date_inspected 的 MONTH 等于该值的 MySQL 常量, return true 并将其添加到总数中,否则什么也不做。

More on MySQL CASE

between 必须先使用较小的值,而不是相反。变化

BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)

BETWEEN  Date_add(NOW(), interval - 12 month) and NOW()

month() returns 月份的数字而不是名称。

Juergen 在您的查询中有一个问题是正确的。另一个是 MONTH() returns 一个数字,而不是一个字符串。

并且,您可以进一步简化查询。不需要子查询:

    SELECT SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END)) AS 'Januari',
           SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END)) AS 'Februari',
           . . .
    FROM inspection
    WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND NOW();