Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 当当月没有数据时,返回0进行计数_Sql - Fatal编程技术网

Sql 当当月没有数据时,返回0进行计数

Sql 当当月没有数据时,返回0进行计数,sql,Sql,我试着找回12行,从这个月起每个月1行。 我用它来填充一个图表,但由于没有去年年初的数据,该图表显示7月份的数据为1月份的数据(第一行数据也是如此) 如图所示,蓝线在9月份结束。这实际上是2月份的数据。三月(第一个数据点)实际上是七月的数据 因此,如果没有该月的数据,我需要能够将COUNT返回为0 我已经创建了一个ref_months表,其中包含12条记录,每个记录一个月1-12月 我有以下疑问: SELECT appointment.time AS appointmentdateti

我试着找回12行,从这个月起每个月1行。 我用它来填充一个图表,但由于没有去年年初的数据,该图表显示7月份的数据为1月份的数据(第一行数据也是如此)

如图所示,蓝线在9月份结束。这实际上是2月份的数据。三月(第一个数据点)实际上是七月的数据

因此,如果没有该月的数据,我需要能够将COUNT返回为0

我已经创建了一个ref_months表,其中包含12条记录,每个记录一个月1-12月

我有以下疑问:

SELECT
    appointment.time AS appointmentdatetime,
    ref_months.text AS ref_month_text,
    ref_months.month AS ref_month_int,
    YEAR(TIME) AS appointmentyear,
    COUNT(appointment.id) AS COUNT
FROM
    appointment
RIGHT OUTER JOIN ref_months ON ref_months.month = MONTH(appointment.time)
WHERE
    appointment.time >= DATE_ADD(NOW(), INTERVAL - 12 MONTH)
GROUP BY
    ref_months.month
ORDER BY
    appointmentyear ASC,
    ref_month_int ASC
当前返回:

+---------------------+----------------+------------------+--------------------+-------+--+
| appointmentdatetime | ref_month_text | ref_month_int    | appointmentyear    | COUNT |  |
+---------------------+----------------+------------------+--------------------+-------+--+
| 2019-07-27 13:00:00 | July           |                7 |               2019 |     1 |  |
| 2019-08-26 13:00:00 | August         |                8 |               2019 |     2 |  |
| 2019-09-06 13:00:00 | September      |                9 |               2019 |     8 |  |
| 2019-10-22 12:00:00 | October        |               10 |               2019 |     9 |  |
| 2019-11-21 12:00:00 | November       |               11 |               2019 |    15 |  |
| 2019-12-27 11:00:00 | December       |               12 |               2019 |     2 |  |
| 2020-01-22 15:00:00 | January        |                1 |               2020 |     4 |  |
| 2020-02-12 09:00:00 | February       |                2 |               2020 |     1 |  |
+---------------------+----------------+------------------+--------------------+-------+--+
我需要返回的是(过去12个月,如果没有数据,则显示计数为0):


我尝试了左、右、内部联接的每一种变体,但仍然没有返回空行。

我强烈建议
左联接
,从要保留所有内容的表开始。然后,要非常小心
WHERE
子句中的内容

所以,我想你想要:

SELECT a.time AS appointmentdatetime,
       m.text AS ref_month_text,
       m.month AS ref_month_int,
       YEAR(TIME) AS appointmentyear,
       COUNT(a.id) AS COUNT
FROM ref_months m LEFT JOIN
     appointment a
     ON r.month = MONTH(a.time) AND
        a.time >= DATE_ADD(NOW(), INTERVAL - 12 MONTH)
GROUP BY m.month
ORDER BY appointmentyear ASC, ref_month_int ASC;
您的
WHERE
子句正在撤消外部联接。我还要注意一些其他事项:

  • 您的
    分组依据
    选择
    中的未聚合列不匹配。这将在大多数数据库中产生语法错误,包括较新版本的MySQL
  • 当前月份将由今年和去年部分填充。我觉得这很奇怪
  • 完全不清楚为什么要在结果集中使用任意值
    a.time
    。这尤其是对
    MIN()
    MAX()
    GROUP\u CONCAT()
    的尖叫
  • appointmentyear
    在没有约会的行上将为
    NULL
    。这似乎有点奇怪

如果您想解决这些问题,我建议您提出一个新问题,并附上适当的样本数据、期望的结果和解释。

用您正在使用的数据库标记您的问题。感谢您的快速回复!我将提出一个新问题
SELECT a.time AS appointmentdatetime,
       m.text AS ref_month_text,
       m.month AS ref_month_int,
       YEAR(TIME) AS appointmentyear,
       COUNT(a.id) AS COUNT
FROM ref_months m LEFT JOIN
     appointment a
     ON r.month = MONTH(a.time) AND
        a.time >= DATE_ADD(NOW(), INTERVAL - 12 MONTH)
GROUP BY m.month
ORDER BY appointmentyear ASC, ref_month_int ASC;