13

我有一个表格供我的用户使用,其中有一个名为“created”的字段,其中包含注册日期。

如何获取包含过去 12 个月内每月注册数计数的列表?像这样:

Month   Count
1        1232
2        2222
3         122
4        4653
...       ...
12       7654

我不习惯使用 mysql,所以直到现在我只知道如何计算去年的注册数量,而不是如何将过去 12 个月的数量分组。提前致谢!

更新

现在我得到了这个,使用@fthiella 解决方案:

+------------------------------+-------------------------------+----------+
| Year(FROM_UNIXTIME(created)) | Month(FROM_UNIXTIME(created)) | Count(*) |
+------------------------------+-------------------------------+----------+
|                         2012 |                             4 |     9927 |
|                         2012 |                             5 |     5595 |
|                         2012 |                             6 |     4431 |
|                         2012 |                             7 |     3299 |
|                         2012 |                             8 |      429 |
|                         2012 |                            10 |     3698 |
|                         2012 |                            11 |     6208 |
|                         2012 |                            12 |     5142 |
|                         2013 |                             1 |     1196 |
|                         2013 |                             2 |       10 |
+------------------------------+-------------------------------+----------+

我如何强制查询给我count = 0的月份?

@fthiella 的解决方案(非常感谢!):

     SELECT y, m, Count(users.created)
     FROM (
      SELECT y, m
      FROM
         (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
         (SELECT 1 m 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 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months) ym
       LEFT JOIN users
       ON ym.y = YEAR(FROM_UNIXTIME(users.created))
          AND ym.m = MONTH(FROM_UNIXTIME(users.created))
     WHERE
       (y=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
       OR
       (y<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
     GROUP BY y, m;

结果:

+------+----+----------------------+
| y    | m  | Count(users.created) |
+------+----+----------------------+
| 2012 |  5 |                 5595 |
| 2012 |  6 |                 4431 |
| 2012 |  7 |                 3299 |
| 2012 |  8 |                  429 |
| 2012 |  9 |                    0 |
| 2012 | 10 |                 3698 |
| 2012 | 11 |                 6208 |
| 2012 | 12 |                 5142 |
| 2013 |  1 |                 1196 |
| 2013 |  2 |                   10 |
| 2013 |  3 |                    0 |
| 2013 |  4 |                    0 |
+------+----+----------------------+
4

3 回答 3

17

如果 created 是一个 INT 字段,您应该使用FROM_UNIXTIME函数将其转换为日期字段,然后使用MONTH函数提取月份:

SELECT Month(FROM_UNIXTIME(created)), Count(*)
FROM yourtable
WHERE FROM_UNIXTIME(created) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Month(FROM_UNIXTIME(created))

这将计算过去 12 个月内创建的所有行。请注意,最好也按年份分组:

SELECT Year(FROM_UNIXTIME(created)), Month(FROM_UNIXTIME(created)), Count(*)
FROM yourtable
WHERE FROM_UNIXTIME(created) >= CURDATE() - INTERVAL 1 YEAR
GROUP BY Year(FROM_UNIXTIME(created)), Month(FROM_UNIXTIME(created))

如果您需要计算注册号而不是行数,您可以使用类似

COUNT(registration_number)

跳过空值,或

COUNT(DISTINCT registration_number)

只计算不同的。

编辑

如果您还需要显示 count=0 的月份,我会使用这样的查询来返回当前和上一年的所有月份:

SELECT y, m
FROM
  (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
  (SELECT 1 m 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 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months;

然后我会使用 LEFT JOIN,它返回第一个查询的所有行,并且只返回匹配的第二个查询的行:

SELECT y, m, Count(yourtable.created)
FROM (
  SELECT y, m
  FROM
    (SELECT YEAR(CURDATE()) y UNION ALL SELECT YEAR(CURDATE())-1) years,
    (SELECT 1 m 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 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12) months) ym
  LEFT JOIN yourtable
  ON ym.y = YEAR(FROM_UNIXTIME(yourtable.created))
     AND ym.m = MONTH(FROM_UNIXTIME(yourtable.created))
WHERE
  (y=YEAR(CURDATE()) AND m<=MONTH(CURDATE()))
  OR
  (y<YEAR(CURDATE()) AND m>MONTH(CURDATE()))
GROUP BY y, m

(请注意,我在这里只考虑过去 12 个月,所以如果我们在 2013 年 4 月中旬,它将在 2012 年 5 月至 4 月 13 日期间计算行数,如果这不是正确的行为,请告诉我)

于 2013-04-02T21:03:18.883 回答
14
SELECT MONTH(reg_date) , COUNT(reg_date) 
FROM your_table
WHERE reg_date >= NOW() - INTERVAL 1 YEAR
GROUP BY MONTH(reg_date)
于 2013-04-02T21:03:28.957 回答
3

如果这对其他人有帮助,我还想要这个查询,但是对于给定的时间间隔,不一定是过去 12 个月(也有日期的时间戳):

set @start='2013-05-01 00:00:00';
set @end='2015-03-31 00:00:00';

SELECT YEAR(FROM_UNIXTIME(yourtable.created_date)), MONTH(FROM_UNIXTIME(yourtable.created_date)), COUNT(yourtable.created_date) 
FROM yourtable
WHERE created_date BETWEEN UNIX_TIMESTAMP( DATE(@start) ) AND UNIX_TIMESTAMP( DATE(@end) )
GROUP BY YEAR(FROM_UNIXTIME(yourtable.created_date)), MONTH(FROM_UNIXTIME(yourtable.created_date))
ORDER BY YEAR(FROM_UNIXTIME(yourtable.created_date)), MONTH(FROM_UNIXTIME(yourtable.created_date));
于 2015-06-24T16:58:04.617 回答