如果count == 0,则SQL返回0

时间:2018-05-13 08:34:06

标签: mysql sql

我想获取Mysql中两个日期之间的所有计数,但是当特定日期没有值时,SQL结果将返回none。但我想返回0。

    SELECT
    DATE(`date`) AS RegistrationDate,   COUNT(`id`) AS NumberOfRegistrations
 FROM
    Users
 WHERE
    `date` between "2018/05/1" and "2018/05/13"
 GROUP BY
    RegistrationDate

2 个答案:

答案 0 :(得分:1)

正如我的评论一样,这是我解决问题的方法 (可能有其他方式)

联接中的第一个表创建了一系列日期。

SELECT
  DATE(`gen_date`) AS RegistrationDate, 
  COUNT(`id`) AS NumberOfRegistrations 
FROM 
  (
    SELECT * 
    FROM
      (SELECT adddate('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) gen_date 
        FROM 
          (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, 
          (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)  t1, 
          (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)  t2,  
          (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)  t3, 
          (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9)  t4
      ) v 
    WHERE 
      gen_date BETWEEN "2018-05-01" AND "2018-05-13"
  ) d 
LEFT OUTER JOIN users u ON Date(`gen_date`) = Date(`date`)
GROUP BY
  gen_date

看到它正常工作hereUpdated

答案 1 :(得分:0)

如果您有日历表,那么问题就解决了:

SELECT DATE(`date`) AS RegistrationDate, COUNT(u.id) AS NumberOfRegistrations
FROM Calendar c LEFT JOIN
     Users u
     ON DATE(u.date) = c.date
WHERE c.date >= '2018-05-01' AND c.date < '2018-05-14'
GROUP BY RegistrationDate;

Calendar表非常有用,你可以找到一个谷歌搜索。

如果碰巧有一个方便的话,你可以用numbers表做类似的事情。

如果没有,您可以生成所需的日期:

SELECT DATE(`date`) AS RegistrationDate, COUNT(u.id) AS NumberOfRegistrations
FROM (SELECT DATE('2018-05-01') as dte UNION ALL
             DATE('2018-05-02') as dte UNION ALL
             DATE('2018-05-03') as dte UNION ALL
             DATE('2018-05-04') as dte UNION ALL
             DATE('2018-05-05') as dte UNION ALL
             DATE('2018-05-06') as dte UNION ALL
             DATE('2018-05-07') as dte UNION ALL
             DATE('2018-05-08') as dte UNION ALL
             DATE('2018-05-09') as dte UNION ALL
             DATE('2018-05-10') as dte UNION ALL
             DATE('2018-05-11') as dte UNION ALL
             DATE('2018-05-12') as dte UNION ALL
             DATE('2018-05-13') as dte UNION ALL
             DATE('2018-05-14') as dte
     ) c LEFT JOIN
     Users u
     ON DATE(u.date) = c.dte
WHERE c.dte >= '2018-05-01' AND c.dte < '2018-05-14'
GROUP BY RegistrationDate;

注意:

  • 使用日期YYYY-MM-DD的标准日期格式。
  • SQL中的标准字符串分隔符是单引号。使用它,除非你有充分的理由使用双引号。
  • 不要将between与日期一起使用。目前还不清楚您是否打算包含2018-05-13,但您的查询可能会排除该日期的任何数据(由于时间成分)。