Mysql查询每个月得到计数

时间:2013-04-02 21:00:38

标签: mysql count group-by

我的用户表有一个名为“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 |
+------+----+----------------------+

3 个答案:

答案 0 :(得分:10)

如果创建的是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日之间的行数,如果这不是正确的行为请告诉我)

答案 1 :(得分:9)

SELECT MONTH(reg_date) , COUNT(reg_date) 
FROM your_table
WHERE reg_date >= NOW() - INTERVAL 1 YEAR
GROUP BY MONTH(reg_date)

答案 2 :(得分:0)

如果这对其他人有帮助,我还想要这个查询,但是对于给定的时间间隔,不一定是过去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));