[英]MySQL number of rows per time
I'm trying to build a query that shows the total number of users for every month/year. 我正在尝试建立一个查询,以显示每个月/年的用户总数。 The query I have right now shows me the users created in any particular month/year point. 我现在拥有的查询向我显示了在任何特定月份/年份中创建的用户。 But what I want is the sum of users till that point, not just the ones created in that period of time. 但是我想要的是到那时为止的用户总数,而不仅仅是那段时间创建的用户总数。 This is the table structure: 这是表结构:
mysql> desc users;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | int(11) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| legajo | int(10) unsigned | NO | | NULL | |
| doc_tipo | varchar(45) | NO | | NULL | |
| doc_nro | int(10) unsigned | NO | | NULL | |
| activo | int(10) unsigned | NO | | 0 | |
| token_actv | varchar(255) | NO | | NULL | |
| token_fecha_envio | datetime | NO | | NULL | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
| role | varchar(60) | NO | | usuario | |
| nombres | varchar(255) | YES | | NULL | |
| apellido | varchar(255) | YES | | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
This is the query. 这是查询。
SELECT CONCAT_WS('/',Month(created),YEAR(created)), Count(*)
FROM users
GROUP BY Month(created),YEAR(created)
ORDER BY Month(created) ASC ,YEAR(created) ASC
Results from current query looks like: 当前查询的结果如下:
mysql> SELECT CONCAT_WS('/',Month(created),YEAR(created)), Count(*) FROM users GROUP BY Month(created),YEAR(created) ORDER BY Month(created) ASC ,YEAR(created) ASC LIMIT 5;
+---------------------------------------------+----------+
| CONCAT_WS('/',Month(created),YEAR(created)) | Count(*) |
+---------------------------------------------+----------+
| 1/2010 | 79 |
| 1/2011 | 70 |
| 1/2012 | 70 |
| 1/2013 | 80 |
| 1/2014 | 64 |
+---------------------------------------------+----------+
Any help will be greatly appreciated. 任何帮助将不胜感激。
All you need to do is use the query you have, as the input to another query, and then use a mysql variable to hold the cumulative sum - lets say you alias your year/month string as d, and your count value as c: 您需要做的就是使用您拥有的查询作为另一个查询的输入,然后使用mysql变量来保存累计和-假设您将年/月字符串别名为d,并将计数值别名为c:
select d , @sum := @sum + c as rolling_total from (
SELECT CONCAT_WS('/',Month(created),YEAR(created)) d, Count(*) c
FROM users
GROUP BY Month(created),YEAR(created)
ORDER BY Month(created) ASC ,YEAR(created) ASC
) q cross join (select @sum := 0) qq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.