获取按月和年分组的 mysql table 中的总行数

Get total of rows in a mysql table grouped by months and years

我想创建一些统计数据,我希望将它们放在按月和年分组的数组中。

得到 MySQL table_usersuser_id。 我想建立一个 mysql 查询来列出每个月底我们总共有多少成员。查询的结果应该是:

 Year:   Month:    Members:
 --------------------------
 2014     12        11345
 2015      1        17939
 2015      2        25003
 2015      3        32667

添加 user_id 时,还有带有 UNIX 时间戳的列 user_signupdate。到目前为止,无论我尝试过什么,我都只得到 user_id 的增长,但我想得到我们每个月和每年的所有 user_id 的总数。

是否可以只用一个 MySQL 查询来计数和分组?

以下代码将执行简单的算术计算以生成成员 运行 总数。参见 SQL Fiddle demo

select 
t1.year,
t1.month,
(@rtotal := @rtotal + t1.members) AS members
from 
(select year(user_signupdate) as year, month(user_signupdate) as month, count(user_id) as members
from table_users
group by year(user_signupdate), month(user_signupdate)
order by year(user_signupdate), month(user_signupdate)) as t1, (Select @rtotal:=0) as rt