即使没有相应的mysql结果,如何计算?

时间:2013-08-23 07:38:54

标签: mysql sql

我正在形成一个查询,以提供按日期排序的去年提交的报告数量。 我用php获得当前年份和月份:

$year = date('Y') - 1;
$month = date('m');

并执行以下查询: SQL:

SELECT month(date_lm) AS `month` ,
count(*) AS `count`
FROM `reports` 
WHERE (status = 'submitted') 
AND (date_lm > 2012-08) 
GROUP BY month(date_lm) 
ORDER BY month(date_lm) ASC

因为在去年只提交了1个,所以只给了我1个结果......

| month  |  count  |
|   7    |    1    |

但我希望结果集显示:

| month  |  count  |
|   9    |    0    |
|   10   |    0    |
|   11   |    0    |
|   12   |    0    |
|   1    |    0    |
|   2    |    0    |
|   3    |    0    |
|   4    |    0    |
|   5    |    0    |
|   6    |    0    |
|   7    |    1    |
|   8    |    0    |

这可能吗?

4 个答案:

答案 0 :(得分:3)

为了做到这一点,你可以创建一个'月'表,然后在该表和报告表之间使用左外连接。

如果语法稍微关闭,我从来没有使用过mysql,所以这就是查询:

SELECT months.monthNumber,
    count(reports.id) AS `count`
FROM `months` left outer join `reports` on months.monthNumber = month(reports.date_lm) 
WHERE (status = 'submitted') 
AND (date_lm > 2012-08) 
GROUP BY monthNumber
ORDER BY monthNumber ASC

重要的是,计数应该是报告表中的一列,而不是月份表,否则你永远不会得到零。

答案 1 :(得分:3)

count(col_name)AS count会给你数0

供参考,您可以访问http://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/

答案 2 :(得分:2)

你应该用1..12表左键加入这个表。 像这样:

SELECT  Months.id AS `month` ,
COUNT(`reports`.date_lm) AS `count`
FROM 
(
  SELECT 1 as ID UNION SELECT 2 as ID UNION  SELECT 3 as ID UNION SELECT 4 as ID 
  UNION  
  SELECT 5 as ID UNION SELECT 6 as ID UNION SELECT 7 as ID UNION SELECT 8 as ID 
  UNION  
  SELECT 9 as ID UNION SELECT 10 as ID UNION SELECT 11 as ID UNION SELECT 12 as ID
) as Months
LEFT JOIN `reports` on Months.id=month(`reports`.date_lm)
                       AND 
                       (status = 'submitted') 
                       AND (date_lm > 2012-08)
GROUP BY Months.id 
ORDER BY Months.id ASC

SQL Fiddle demo

答案 3 :(得分:0)

希望这可以帮助..

SELECT t1.month_year AS month, COALESCE(SUM(t1.total+t2.total), 0) AS count FROM ( SELECT DATE_FORMAT(a.Date, "%Y-%m") AS md, DATE_FORMAT(a.Date, "%b-%y") AS month_year, '0' AS total FROM ( SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY AS Date FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c ) a WHERE a.Date <= NOW() AND a.Date >= Date_add(Now(),INTERVAL - 11 MONTH) GROUP BY md )t1 LEFT JOIN ( SELECT DATE_FORMAT(created_at, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(created_at, "%Y-%m") AS md FROM reports WHERE created_at <= NOW() AND created_at >= Date_add(Now(),INTERVAL - 11 MONTH) AND {状态{1}}

这将提供过去12个月的数据,即使没有数据为0 ..