MySQL按月选择为所有月份返回NULL

时间:2016-07-28 11:36:59

标签: mysql sql date null mysql-5.6

使用MySQL 5.6我试图在过去12个月中为每个inspections选择总month,如果没有,则输出为0。我似乎在这里遗漏了一些东西,因为输出都是NULLdate_inspected字段只是常规SQL date

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

表:

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常量对于该值,返回true并将其添加到总计中,否则不执行任何操作。

More on MySQL CASE

2 个答案:

答案 0 :(得分:2)

sale.order.line必须首先使用较小的值,而不是相反。变化

between

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

BETWEEN Date_add(NOW(), interval - 12 month) and NOW() 返回月份的数字,而不是名称。

答案 1 :(得分:2)

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

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

    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();