我正在编写一个查询获取区域图表的结果。我有两年的数据。我按日期使用mysql分组获取每日结果,但是当我按周使用分组icannot找到一种方法来分离两周每周数据becoz它返回周数如4,5
我的代码是
$results=mysql_query("SELECT SUM(rate) as rate, week(rdate) as rdate
FROM cpustats
WHERE server_id='$server_id'
GROUP BY week(rdate)") or die(mysql_error());
$data3='';
$data4='';
while ($row = mysql_fetch_array($results)) {
//one day has lots of records i devide that for get average
$rk=mysql_query("SELECT rate as rdate FROM cpustats
WHERE server_id='$server_id'
AND week(rdate)='".$row['rdate']."'") or die(mysql_error());
$nr=mysql_num_rows($rk);
//here has issue which shows sum of two years same week data
$usage=round($row['rate']/$nr,2);
//tfunction return start and end of week by passing week number
$week=getStartAndEndDate($row['rdate']);
$data3[] = "'".$week."'";
$data4[] = "$usage";
}
任何人都可以帮助我获得两年的单独数据吗
答案 0 :(得分:0)
您还需要按年份添加组,以便将结果分为星期和年份
SELECT
SUM(rate) as rate,
WEEK(rdate) as rdate ,
YEAR(rdate) as rdate_year
FROM cpustats
WHERE server_id='$server_id'
GROUP BY YEAR(rdate),WEEK(rdate)