use_id reg_time1 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
To get
`
reg_time count count(2周内)2015-12-01 0 02015-11-29 0 02015-11-30 1 12015-11-31 0 12015-12-01 1 22015-12-02 2 42015-12-03 3 7
`
The purpose is to take out the same day registration and push forward 14 days of registration number
The current practice is to cycle 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; }
`
The result is a total of 28 executions. Count sense efficiency is quite low for optimal thinking
Reply content:
use_id reg_time1 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
To get
`
reg_time count count(2周内)2015-12-01 0 02015-11-29 0 02015-11-30 1 12015-11-31 0 12015-12-01 1 22015-12-02 2 42015-12-03 3 7
`
The purpose is to take out the same day registration and push forward 14 days of registration number
The current practice is to cycle 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; }
`
The result is a total of 28 executions. Count sense efficiency is quite low for optimal thinking
select reg_time,count(1) from table where reg_time > $last and reg_time < $last group by reg_time
Group queries with SQL
For your problem, it is recommended that you directly get the daily update data, in PHP to add a bit. So that SQL can be reduced to only one left.