每个月获得记录,但如果没有那个月的记录也会得到零

时间:2017-01-19 14:30:22

标签: mysql

如果我有以下SQL表

测试

id   type   receiveDate
 1   Blood   2012-01-18
 2   Blood   2012-01-20
 3   Blood   2012-01-18
 4   Blood   2012-03-01
 5   Blood   2012-05-21
 6   Blood   2012-05-18
 7   Blood   2012-06-14
 8   Blood   2012-07-22
 9   Blood   2012-07-29
 10  Blood   2012-12-04
 11  Blood   2012-12-30 

我运行查询

SELECT COUNT(*)
FROM Tests 
WHERE YEAR(receiveDate) = '2012' 
GROUP BY MONTH(receiveDate)

我会回来的

COUNT(*)
 3
 1
 2
 1
 2
 2

我的问题是,即使那个月没有记录,有没有办法在12个月内找回?所以例如我想得到

的输出
COUNT(*)
 3
 0
 1
 0
 2
 1
 2
 0
 0
 0
 0
 2

此外,作为一个额外的奖励是有一种方法显示计数月? 谢谢!

2 个答案:

答案 0 :(得分:2)

SELECT  Months.m AS month, COUNT(Tests.receiveDate) AS total FROM 
(
    SELECT 1 as m 
    UNION SELECT 2 as m 
    UNION SELECT 3 as m 
    UNION SELECT 4 as m 
    UNION SELECT 5 as m 
    UNION SELECT 6 as m 
    UNION SELECT 7 as m 
    UNION SELECT 8 as m 
    UNION SELECT 9 as m 
    UNION SELECT 10 as m 
    UNION SELECT 11 as m 
    UNION SELECT 12 as m
) as Months
LEFT JOIN Tests  on Months.m = MONTH(Tests.receiveDate)  
GROUP BY
    Months.m

如果您想要特定年份,请尝试此操作。

SELECT  Months.m AS month, COUNT(Tests.receiveDate) AS total FROM 
(
    SELECT 1 as m 
    UNION SELECT 2 as m 
    UNION SELECT 3 as m 
    UNION SELECT 4 as m 
    UNION SELECT 5 as m 
    UNION SELECT 6 as m 
    UNION SELECT 7 as m 
    UNION SELECT 8 as m 
    UNION SELECT 9 as m 
    UNION SELECT 10 as m 
    UNION SELECT 11 as m 
    UNION SELECT 12 as m
) as Months
LEFT JOIN Tests on Months.m = MONTH(Tests.receiveDate)  
AND YEAR(Tests.receiveDate) = '2012'
GROUP BY Months.m

答案 1 :(得分:0)

这是一种替代方法,可以将一年中的所有结果排成一行。

SELECT 
    SUM(if(MONTH(receiveDate) = 1, 1,0)) as Jan,
    SUM(if(MONTH(receiveDate) = 2, 1,0)) as Feb,
    SUM(if(MONTH(receiveDate) = 3, 1,0)) as Mar,
    SUM(if(MONTH(receiveDate) = 4, 1,0)) as Apr,
    SUM(if(MONTH(receiveDate) = 5, 1,0)) as May,
    SUM(if(MONTH(receiveDate) = 6, 1,0)) as Jun,
    SUM(if(MONTH(receiveDate) = 7, 1,0)) as Jul,
    SUM(if(MONTH(receiveDate) = 8, 1,0)) as Aug,
    SUM(if(MONTH(receiveDate) = 9, 1,0)) as Sep,
    SUM(if(MONTH(receiveDate) = 10, 1,0)) as Oct,
    SUM(if(MONTH(receiveDate) = 11, 1,0)) as Nov,
    SUM(if(MONTH(receiveDate) = 12, 1,0)) as `Dec`
FROM test 
WHERE YEAR(receiveDate) = '2012' 
  AND type = 'Blood'

结果是

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
3   0   1   0   2   1   2   0   0   0   0   2

如果您想报告一年中的多种类型

SELECT type,
    SUM(if(MONTH(receiveDate) = 1, 1,0)) as Jan,
    SUM(if(MONTH(receiveDate) = 2, 1,0)) as Feb,
    SUM(if(MONTH(receiveDate) = 3, 1,0)) as Mar,
    SUM(if(MONTH(receiveDate) = 4, 1,0)) as Apr,
    SUM(if(MONTH(receiveDate) = 5, 1,0)) as May,
    SUM(if(MONTH(receiveDate) = 6, 1,0)) as Jun,
    SUM(if(MONTH(receiveDate) = 7, 1,0)) as Jul,
    SUM(if(MONTH(receiveDate) = 8, 1,0)) as Aug,
    SUM(if(MONTH(receiveDate) = 9, 1,0)) as Sep,
    SUM(if(MONTH(receiveDate) = 10, 1,0)) as Oct,
    SUM(if(MONTH(receiveDate) = 11, 1,0)) as Nov,
    SUM(if(MONTH(receiveDate) = 12, 1,0)) as `Dec`
FROM test 
WHERE YEAR(receiveDate) = '2012'
GROUP by type;

结果是:

type    Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Blood   3   0   1   0   2   1   2   0   0   0   0   2
Sweat   2   1   0   0   1   0   0   0   0   0   0   0
Tears   0   0   1   2   0   0   0   0   0   0   0   0