MySQL行组查询

时间:2015-02-21 18:08:45

标签: php mysql datetime report

我有一张像

这样的表格
id    |    name    |    registered_date    |  
------+------------+-----------------------+--
4     |  Name 1    |  2015-02-15 00:00:00  |
5     |  Name 1    |  2014-02-15 00:00:00  |
6     |  Name 1    |  2015-02-15 00:00:00  |
7     |  Name 1    |  2013-02-15 00:00:00  |
8     |  Name 1    |  2015-02-15 00:00:00  |
9     |  Name 1    |  2015-02-15 00:00:00  |
10    |  Name 1    |  2015-02-15 00:00:00  |
11    |  Name 1    |  2015-02-15 00:00:00  |
12    |  Name 1    |  2015-02-15 00:00:00  |
13    |  Name 1    |  2015-02-15 00:00:00  |
14    |  Name 1    |  2015-02-15 00:00:00  |

in' registered_date"保留用户注册日期的字段

现在我需要记录每个月注册的用户数量。

是否可以使用当前结构?结果将是这样的

totalCOunt    |     momthYear
--------------+----------------
144           |     Feb-2015
--------------+----------------
100           |     Mar-2015
--------------+----------------
104           |     Apr-2015
--------------+----------------
174           |     May-2015
--------------+----------------
144           |     June-2015
--------------+----------------

提前致谢

1 个答案:

答案 0 :(得分:2)

SELECT COUNT(*) AS totalCount, DATE_FORMAT(registered_date, '%M-%Y') AS momthYear FROM tablename GROUP BY by momthYear ORDER BY momthYear