Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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
Mysql 过去12个月的月度创建账户_Mysql_Sql - Fatal编程技术网

Mysql 过去12个月的月度创建账户

Mysql 过去12个月的月度创建账户,mysql,sql,Mysql,Sql,我有一个帐户表,其中一个名为“dateCreation”的列是创建帐户的日期 我想得到所有创建的帐户在过去12个月,但有时没有帐户创建了2个月或更长的时间。在这几个月里,我喜欢mysql返回0。我的请求工作正常(我在stackoverflow上获得): 但它会返回这样的结果: ID | Count 1 | 0 2 | 0 3 | 0 4 | 0 5 |

我有一个帐户表,其中一个名为“dateCreation”的列是创建帐户的日期

我想得到所有创建的帐户在过去12个月,但有时没有帐户创建了2个月或更长的时间。在这几个月里,我喜欢mysql返回0。我的请求工作正常(我在stackoverflow上获得):

但它会返回这样的结果:

    ID    |    Count
    1     |      0
    2     |      0
    3     |      0
    4     |      0
    5     |      8
    6     |      1
    7     |      0
    8     |      1
    9     |      0
    10    |      7
    11    |      0
    12    |      4
但是我们在九月,所以我希望第一个月是十月

这里有一个SQLFIDLE:

修改您的订单以:

ORDER BY
  CASE WHEN Months.id > MONTH(NOW()) -- check if the month is after the current 
       THEN 0                        -- order by months after the current first
       ELSE 1                        -- and then by the months up to the current 
  END,
  Months.id ASC

请参见

请尝试替换左连接:

LEFT JOIN customers ON Months.id=MONTH(customers.dateCreation) 
    AND dateCreation BETWEEN DATE_ADD(CURDATE(),INTERVAL - 12 MONTH) AND CURDATE()

@Alexking2005:添加更多explanation@Alexking2005这似乎是不言自明的。你在挣扎什么?
LEFT JOIN customers ON Months.id=MONTH(customers.dateCreation) 
    AND dateCreation BETWEEN DATE_ADD(CURDATE(),INTERVAL - 12 MONTH) AND CURDATE()