for($i=1;$i<=12;$i++){
($i<10)? $m = '0'.$i : $m = $i; #หาเดือน
$chkDate = '2013-'.$m; #วันที่เอาไปเช็ค
$sql = mysql_query(" SELECT gen, SUM(size) AS sumSize FROM ตาราง WHERE date = '$chkDate' GROUP BY gen ORDER BY date ASC");
if(mysql_num_rows($sql)>0){
$result = mysql_fetch_assoc($sql);
echo 'Gen = '.$result['gen'].' Date = '.$chkDate.' Sum = '.$result['sumSize'].'<hr>';
} else {
echo 'Gen = '.$result['gen'].' Date = '.$chkDate.' Sum = 0 <hr>';
}
}
Date :
2013-12-17 14:51:17
By :
arm8957
No. 4
Guest
ได้แล้ว ครับ แต่ค่า sum ไม่ตรง ครับ เหมือน วน ค่าเดิม ครับ
Gen = A Date = 2013-01 Sum = 2
Gen = A Date = 2013-02 Sum = 2
Gen = A Date = 2013-03 Sum = 2
Gen = A Date = 2013-04 Sum = 2
Gen = A Date = 2013-05 Sum = 2 // 6
Gen = A Date = 2013-06 Sum = 2 // 2
Gen = A Date = 2013-07 Sum = 2 // 3
Gen = A Date = 2013-08 Sum = 2
Gen = A Date = 2013-09 Sum = 2
Gen = A Date = 2013-10 Sum = 2
Gen = A Date = 2013-11 Sum = 2
Gen = A Date = 2013-12 Sum = 2
Code (PHP)
<?php
for($i=1;$i<=12;$i++){
($i<10)? $m = '0'.$i : $m = $i; #หาเดือน
$chkDate = '2013-'.$m; #วันที่เอาไปเช็ค
$sql = mysql_query("SELECT gen AS gen , SUM(size) AS sumSize
FROM table01
WHERE date = '$chkDate' or gen = 'A'
GROUP BY gen ORDER BY date ASC");
if(mysql_num_rows($sql) > 0){
$result = mysql_fetch_assoc($sql);
if($order_date !=" "){
echo 'Gen = '.$result['gen'].' Date = '.$chkDate.' Sum = '.$result['sumSize'].'<hr>';
} else {
echo 'Gen = '.$result['gen'].' Date = '.$chkDate.' Sum = 0 <hr>';
}
}
}