Php 获取日期范围的平均最后4次计数

Php 获取日期范围的平均最后4次计数,php,mysql,sql,Php,Mysql,Sql,我对某个日期范围内每天注册的用户进行了查询计数 select count(*) as c, date(from_unixtime(user.firstaccess)) as date from user where firstaccess between ? and ? group by date(from_unixtime(user.firstaccess)) 这很有效。 现在,我正在生成一份报告,我想知道一周最后4天的平均值。例如:今天是星期三,所以我会得到最后4个

我对某个日期范围内每天注册的用户进行了查询计数

select count(*) as c, date(from_unixtime(user.firstaccess)) as date
    from user
    where firstaccess between ? and ?
    group by date(from_unixtime(user.firstaccess))
这很有效。 现在,我正在生成一份报告,我想知道一周最后4天的平均值。例如:今天是星期三,所以我会得到最后4个星期三,然后得到平均值。 同样,我有这个查询,可以工作,但我可以思考每天单独查询的逻辑。而且没有日期范围。 这就是我所拥有的

select
(
(select count(*) from user where firstaccess between 1456617600-(3600*24*7) and 1456703999-(3600*24*7)) + 
(select count(*) from user where firstaccess between 1456617600-(3600*24*14) and 1456703999-(3600*24*14)) +
(select count(*) from user where firstaccess between 1456617600-(3600*24*14) and 1456703999-(3600*24*14)) +
(select count(*) from user where firstaccess between 1456617600-(3600*24*21) and 1456703999-(3600*24*21))
)
/4
as c
from user
limit 0,1

我使用的是Mysql和PHP

如果您想了解今天、7天前、14天前和21天前首次注册的平均用户数,您可以使用:

select AVG(column_name) as c, date(from_unixtime(user.firstaccess)) as date
    from user
    where firstaccess between ? and ?
    group by date(from_unixtime(user.firstaccess))
select count(*)/4 as avg_new_users
from
user
where 
date(from_unixtime(user.firstaccess))=CURDATE() or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 7 DAY) or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 14 DAY) or date(from_unixtime(user.firstaccess))=DATE_SUB(CURDATE(),INTERVAL 21 DAY)

漂亮的SQL!!它在特定日期有效。虽然仍然不适用于日期范围。您是否有一个表,用于从中提取日期范围(其中包含所有日期?)我实际上没有。有一张有日期的桌子不是很奇怪吗?尽管我放弃了在一次查询中获取所有这些信息。我在foreach内部查询并缓存结果。我尝试了您的查询,但由于性能问题,不得不回滚到我的查询。因为日期函数不能使用索引,所以查询速度比我使用时间戳时慢得多。我感谢你的时间