• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql – php可以按天连续去两周的更新数据吗

php 搞代码 4年前 (2022-01-25) 23次浏览 已收录 0个评论
文章目录[隐藏]
<code>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   </code>

要得到
`

<code>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</code>

`

目的就是取出当天注册 以及当天往前推14天的注册数量

现在的做法是循环count
`

<code>$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;        }</code>

`

这样做的结果就是一共执行了28次count 感觉效率相当低下 求优化思路

!本文来源gaodai.ma#com搞#代!码(网

搞gaodaima代码

回复内容:

<code>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   </code>

要得到
`

<code>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</code>

`

目的就是取出当天注册 以及当天往前推14天的注册数量

现在的做法是循环count
`

<code>$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;        }</code>

`

这样做的结果就是一共执行了28次count 感觉效率相当低下 求优化思路

<code>select reg_time,count(1) from table where reg_time > $last and reg_time < $last group by reg_time</code>

用sql分组查询

针对你的问题,建议你直接获取每天的更新数据,在PHP自己加一下。这样SQL能减少到只剩一个。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql – php可以按天连续去两周的更新数据吗

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址