我有有跟踪用户的表中的Drupal站点。 我想要做的是随着时间的推移图表会员增长。 所以我想mysql的按摩到返回是这样的:
date | # of users (total who have registered up to the given date)
1/1/2014 | 0
1/2/2014 | 2
1/3/2014 | 10
其中,“用户#”是已注册账户到指定日期的用户(运行计)总数-而不是用户的谁的是,当日登记的数量 (这是微不足道的检索)。
我的每一行{users}
表中有一个uid
列,一个name
列和created
(时间戳)列。
所以从我的一个样本记录{users}
表将是:
name: John Smith
uid: 526
created: 1365844220
尝试:
select u.created, count(*)
from (select distinct date(created) created from `users`) u
join `users` u2 on u.created >= date(u2.created)
group by u.created
SQLFiddle 这里 。
我结束了使用结合变量的基础上,堆栈溢出的答案发布的解决方案在这里 。 该解决方案似乎是有点比提供的其他答案更加灵活和高效。
SELECT u.date,
@running_total := @running_total + u.count AS count
FROM (
SELECT COUNT(*) AS count, DATE_FORMAT(FROM_UNIXTIME(created), '%b %d %Y') AS date
FROM {users}
WHERE created >= :start_time AND created <= :end_time
GROUP BY YEAR(FROM_UNIXTIME(created)), MONTH(FROM_UNIXTIME(created)), DAY(FROM_UNIXTIME(created))
) u
JOIN (
SELECT @running_total := u2.starting_total
FROM (
SELECT COUNT(*) as starting_total
FROM {users}
WHERE created < :start_time
) u2
) initialize;
需要注意的是按组,日期格式和范围的要求仅仅是我的特定项目的细节。 此溶液(按照原来的问题)的更通用的形式是:
SELECT u.date,
@running_total := @running_total + u.count AS count
FROM (
SELECT COUNT(*) AS count, DATE(FROM_UNIXTIME(created)) AS date
FROM {users}
GROUP BY date
) u
JOIN (
SELECT @running_total := 0
) initialize;
不知道表结构,调整查询到你的需求
SELECT DATE(created), COUNT(*) AS Users FROM users GROUP BY DATE(created)
当你只想显示有registerd用户添加的日期
HAVING COUNT(*) > 0
在与查询