php - 统计一个数据表并求和,用默认值填充一个空数据

标签 php mysql sql database

我有一个只包含日期数据的表,我想统计数据在同一个月的次数,对于那个问题它已经完成了,现在我遇到了一个新问题,就是缺少月份,这是因为那个月没有数据。现在我想添加默认值为 0 的空月份,即使该月份不在表中。有人可以根据我的查询帮助我吗?

这是我的数据

start_date  |end_date
------------------------
 2018-10-01 |2018-10-02
 2018-01-04 |2018-02-04
 2018-08-01 |2018-10-01

这是我的查询

    select month(month_table) as month_table
         , sum(cstart) as cstart 
         , sum(cend) as cend 
      from 
         (
          (select `start_date` as month_table
                , 1 as cstart
                , 0 as cend 
             from newdata
          ) 
  union all 
          ( select `end_date`
                 , 0
                 , 1 
              from newdata 
           ) 
          ) dd 
      group 
         by monthname(month_table)
          , month(month_table) 
      order 
         by month(month_table)

结果是

month_table|cstart|cend
      1      |  1   |  0
      2      |  0   |  1
      8      |  1   |  0
      10     |  1   |  2

我想添加新的查询,这样我的结果就是

 month_table|cstart|cend
      1      |  1   |  0
      2      |  0   |  1
      3      |  0   |  0
      4      |  0   |  0
      5      |  0   |  0
      6      |  0   |  0
      7      |  0   |  0
      8      |  1   |  0
      9      |  0   |  0
      10     |  1   |  2
      11     |  0   |  0
      12     |  0   |  0

这是我的 fiddle http://sqlfiddle.com/#!9/c18b5f/3/0

最佳答案

您需要一张包含所有月份的表格。您可以使用 UNION ALL 创建一个带有子查询的临时查询。然后将计数子查询左连接到该表。

SELECT m.month month_table,
       coalesce(s.count, 0) cstart,
       coalesce(e.count, 0) cend
       FROM (SELECT 1 month
             UNION ALL
             SELECT 2 month
             UNION ALL
             SELECT 3 month
             UNION ALL
             SELECT 4 month
             UNION ALL
             SELECT 5 month
             UNION ALL
             SELECT 6 month
             UNION ALL
             SELECT 7 month
             UNION ALL
             SELECT 8 month
             UNION ALL
             SELECT 9 month
             UNION ALL
             SELECT 10 month
             UNION ALL
             SELECT 11 month
             UNION ALL
             SELECT 12 month) m
            LEFT JOIN (SELECT month(n.start_date) month,
                              count(*) count
                              FROM newdata n
                              GROUP BY month(n.start_date)) s
                      ON s.month = m.month
            LEFT JOIN (SELECT month(n.end_date) month,
                              count(*) count
                              FROM newdata n
                              GROUP BY month(n.end_date)) e
                      ON e.month = m.month
       ORDER BY m.month;

关于php - 统计一个数据表并求和,用默认值填充一个空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52631882/

相关文章:

mysql - 如何订购此 UNION MySQL 查询?

php - SQLSTATE[HY093] : Invalid parameter number - Can't find the duplicate

php - 从 cron 运行 PHP 脚本

php - 在php mysql中显示类别和子类别

mysql 中的 Java 枚举

sql - 计算每天两个日期时间字段之间的平均时差

c# - 如何使用 EntityFramework 核心在插入中强制使用默认值?

php - 在 C++ 中发出加密字符串并在 PHP 中解密

PHP 时间/日期错误

php - 如何根据MySQL中某个字段中某个值出现的次数来获取记录?