Mysql 每天统计注册用户的SQL查询

Mysql 每天统计注册用户的SQL查询,mysql,sql,Mysql,Sql,在试图解决这个问题时,我几乎要发疯了: 在我的应用程序中,用户可以注册和删除自己。创建日期和删除日期作为时间戳保留在数据库中。我需要知道在一段时间内每天有多少注册用户和未被删除的用户 因此,如果我在2012-02-01有10个现有用户,一个用户在2012-02-03删除了帐户,三个用户在2012-02-04注册,两个用户在2012-02-06删除,并查询从2012-02-01到2012-02-07的注册用户总数,我希望得到如下结果: day total_users 20

在试图解决这个问题时,我几乎要发疯了:

在我的应用程序中,用户可以注册和删除自己。创建日期和删除日期作为时间戳保留在数据库中。我需要知道在一段时间内每天有多少注册用户和未被删除的用户

因此,如果我在2012-02-01有10个现有用户,一个用户在2012-02-03删除了帐户,三个用户在2012-02-04注册,两个用户在2012-02-06删除,并查询从2012-02-01到2012-02-07的注册用户总数,我希望得到如下结果:

day total_users 2012-02-01 10 2012-02-02 10 2012-02-03 9 2012-02-04 12 2012-02-05 12 2012-02-06 10 2012-02-07 10
select c.calendar_date, count(*) user_total
from calendar c
left join user u
       on date(u.created_at) <= c.calendar_date and 
          (date(u.deleted_at) > c.calendar_date or u.deleted_at is null)
group by c.calendar_date
日总用户数 2012-02-01 10 2012-02-02 10 2012-02-03 9 2012-02-04 12 2012-02-05 12 2012-02-06 10 2012-02-07 10 这是我的简化用户表的外观:

用户id、用户名称、创建位置、删除位置

获取某一特定日期的已注册和未删除用户数没有问题(这里是2012-02-01,在我的示例中,这将得到10):

选择application.application\u id作为application\u id,计数(user.user\u id)作为user\u总数
从应用程序、用户
其中application.application\u id=user.application\u id
和日期(user.created_at'2012-02-01')或(user.deleted_at为空))
谁知道我如何创建一个查询(没有光标)以获得预期结果?我的数据库是Debian上的MySQL 5.0.51

提前谢谢 Marcus

选择日期(用户创建日期)作为“创建日期”,计数(用户用户id)作为“用户总数”
来自应用程序a
a.application\u id=u.application\u id上的内部联接用户u
其中日期(user.created_at“2012-02-01”)或(user.deleted_at为空)
按日期分组(用户创建时间)
按日期(用户创建时间)订购ASC

如果您有一个包含日期列表的表(称为类似日历的东西),您可以使用如下查询:

day total_users 2012-02-01 10 2012-02-02 10 2012-02-03 9 2012-02-04 12 2012-02-05 12 2012-02-06 10 2012-02-07 10
select c.calendar_date, count(*) user_total
from calendar c
left join user u
       on date(u.created_at) <= c.calendar_date and 
          (date(u.deleted_at) > c.calendar_date or u.deleted_at is null)
group by c.calendar_date

+1.@user1214154,请注意,这不会列出当天没有任何注册用户的日期,计数为0。它只输出表中存在的天数。它不会返回每天注册的用户数,而是返回每天注册的用户数。鉴于问题中所述的示例数据,它不会产生所需的输出-相反,它会在2012-02-01之前产生大量用户和日期(总计10个用户),而在2012-02-01之后不会产生任何用户。非常感谢,这非常有效!我只需将每行中的“I”替换为t0、t1等,我想这是我以前的MySQL服务器的错。@MarcusMuench:Oops,我的错。我已经更正了问题-谢谢。
select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) calendar_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where calendar_date between ? /*start of date range*/ and ? /*end of date range*/