10

我正在制定一个查询,以提供按日期排序的去年提交的报告数量。我用 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

4 回答 4

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

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

于 2013-08-23T07:45:50.523 回答
3

count(col_name) AScount会给你 count 0

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

于 2013-08-23T07:46:15.213 回答
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 演示

于 2013-08-23T07:50:22.097 回答
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 `status` = 1
  GROUP BY md
)t2 
ON t2.md = t1.md
LEFT JOIN
(
  SELECT DATE_FORMAT(date_lm, "%b") AS month, COUNT(*) AS total ,DATE_FORMAT(date_lm, "%Y-%m") AS md
  FROM tbl_users 
  WHERE date_lm <= NOW() AND date_lm >= Date_add(Now(),INTERVAL - 11 MONTH) AND status = 'submitted'
  GROUP BY md
)t3 
ON t3.md = t1.md 
GROUP BY t1.md
ORDER BY t1.md

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

于 2017-11-07T12:37:13.957 回答