表结构为
id               date          cun_off_count           active_off_count       serverid因为时间是外部条件,是可选的,比如用户选择了2011-05-29至2011-06-28把这个时间段党委条件来分周查询,sql如下
select SUM(run_off_count) as count1,SUM(active_off_count) as count2,DATE_FORMAT(date,"%X-%V")dates from active_runoff where date<="2011-06-28" and date>="2011-05-29" group by dates这样便可查询对应的数据,比如
dates        count1     count2
2011-25        10         20
2011-26        10         20
2011-27        10         20而现在我的需求是count2是需要上一周的数据。比如如上结果,我需要2011-26周的count1/2011-25周的count2在php中我是同时查的,用mysql_fetch_array来读取的话,获得都是同一周的数据
所以我想分开来查count1和count2,但是怎样查询这个时间段的每一天上周的数据(同一个自然周内上一周的数据是相同的)
可能是个思想的问题,请帮忙提点下,谢谢!

解决方案 »

  1.   

    用session么请问?可以说详细点吗,谢谢
      

  2.   

    看看mysql的这个weekofyear方法,group by weekofyear(dates)
      

  3.   


    $result = $dbhelper->Excute($sql);
    $num_results=$dbhelper->GetRowCount($result);
    for ($i=0; $i <$num_results; $i++)
    {
    $row=mysql_fetch_array($result);
    if($row['count2']==0)
    {
    $percent="0%";
    } else
    {
    $percent=round(100*($row['count1']/$row['count2']),2)."%";
    }
                echo "<tr><td>".$row['dates']."</td><td>".$percent."</td><td>".$row['count1']."</td><td>".$row['count2']."</td></tr>";
       }应该在哪里储存$row['count2']呢?  
      

  4.   

    select SUM(run_off_count) as count1,SUM(active_off_count) as count2,DATE_FORMAT(date,"%X-%V")dates from active_runoff where date<="2011-06-28" and date>="2011-05-29" group by dates order by dates;查出来以后再通过php处理一下就可以了啊。
    附上我理解的代码,不知道是不是你想要的。
    <?phpfunction processArray($arrOrig){
        $arrRtn = array();
        $lastCount2 = 0;
        foreach($arrOrig as $orig){
            $origCount2 = $orig['count2'];
            $orig['count2'] = !empty($lastCount2) ? $orig['count1']/$lastCount2 : 0;
            $arrRtn[] = $orig;
            $lastCount2 = $origCount2;
        }    return $arrRtn;
    }$arrOrig = array(
                    array('dates'=>'2011-25','count1'=>10,'count2'=>20),
                    array('dates'=>'2011-26','count1'=>10,'count2'=>20),
                    array('dates'=>'2011-27','count1'=>10,'count2'=>20),
                );
    $arrProc = processArray($arrOrig);
    var_dump($arrProc);
    ?>
      

  5.   

    假定这是你查询的结果
    dates count1 count2
    2011-25 10 10
    2011-26 20 20
    2011-27 30 30你应该是要这样的结果
    dates count1 count2
    2011-25 10 
    2011-26 20 10
    2011-27 30 20  是这样的吧?
    你需要多查一周(前一周)
    先读取取一条把 count1 赋给 count2
    然后循环处理
      循环中 当 count2 用完后做 count1 赋给 count2