use_id reg_time 1 2015-12-01 2 2015-12-02 3 2015-12-02 4 2015-12-03 5 2015-12-03 6 2015-12-03 7 2015-11-30
要得到
`
reg_time count count(2周内) 2015-12-01 0 0 2015-11-29 0 0 2015-11-30 1 1 2015-11-31 0 1 2015-12-01 1 2 2015-12-02 2 4 2015-12-03 3 7
`
立即学习“PHP免费学习笔记(深入)”;
目的就是取出当天注册 以及当天往前推14天的注册数量
现在的做法是循环count
`
$lasttime=strtotime(date("Y-m-d",time()))-14*3600*24;
$firsttime=strtotime(date("Y-m-d",time()))+3600*24;
$result = array()
for($i=13;$i>=0;$i--){
$first=date("Y-m-d",$firsttime-$i*3600*24);
$last=date("Y-m-d",$lasttime-$i*3600*24);
$today=date("Y-m-d",time()-$i*24*3600);
$tmp1 = "select count(1) from table where reg_time > $last and reg_time < $last";
$tmp2 = "select count(1) from table where reg_time = $today "
$result[][date] =$today;
$result[][count1] =$tmp1;
$result[][count2] =$tmp2;
}`
立即学习“PHP免费学习笔记(深入)”;
这样做的结果就是一共执行了28次count 感觉效率相当低下 求优化思路
use_id reg_time 1 2015-12-01 2 2015-12-02 3 2015-12-02 4 2015-12-03 5 2015-12-03 6 2015-12-03 7 2015-11-30
要得到
`
reg_time count count(2周内) 2015-12-01 0 0 2015-11-29 0 0 2015-11-30 1 1 2015-11-31 0 1 2015-12-01 1 2 2015-12-02 2 4 2015-12-03 3 7
`
立即学习“PHP免费学习笔记(深入)”;
目的就是取出当天注册 以及当天往前推14天的注册数量
现在的做法是循环count
`
$lasttime=strtotime(date("Y-m-d",time()))-14*3600*24;
$firsttime=strtotime(date("Y-m-d",time()))+3600*24;
$result = array()
for($i=13;$i>=0;$i--){
$first=date("Y-m-d",$firsttime-$i*3600*24);
$last=date("Y-m-d",$lasttime-$i*3600*24);
$today=date("Y-m-d",time()-$i*24*3600);
$tmp1 = "select count(1) from table where reg_time > $last and reg_time < $last";
$tmp2 = "select count(1) from table where reg_time = $today "
$result[][date] =$today;
$result[][count1] =$tmp1;
$result[][count2] =$tmp2;
}`
立即学习“PHP免费学习笔记(深入)”;
这样做的结果就是一共执行了28次count 感觉效率相当低下 求优化思路
select reg_time,count(1) from table where reg_time > $last and reg_time < $last group by reg_time
用sql分组查询
针对你的问题,建议你直接获取每天的更新数据,在PHP自己加一下。这样SQL能减少到只剩一个。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号