select
product,
sum(holding='< 1 week') as `< 1 week`,
sum(holding='> 1 week') as `> 1 week`,
sum(holding='> 5 weeks') as `> 5 weeks`,
...
from table
group by product
//update holding time
$sql_holding = "Update ProductHDD set hold='$timediffwk1' where mid='$mid'";
$result_update = mysql_query($sql_holding);
}
?>
ผลลัพธ์ที่ใช้ code พี่เขียนออกมาได้แบบนี้ครับ
<?
$sql_hold= "select product, hold,
sum(hold='0') as `<1week`,
sum(hold='1') as `>1week`,
sum(hold='2') as `>2weeks`,
sum(hold='3') as `>3weeks`,
sum(hold='4') as `>4weeks`,
sum(hold='5') as `>5weeks`,
sum(hold='6') as `>6weeks`,
sum(hold='7') as `>7weeks`,
sum(hold='8') as `>8weeks`,
sum(hold='9') as `>9weeks`,
sum(hold='10') as `>10weeks`,
COUNT(product) as `Total`
from Inventory group by product ORDER BY Total DESC";
$rs=$db->query('select distinct hold from tablename order by hold');
$sql = "select hold";
while($ro=$rs->fetch_assoc()){
$h = $ro['hold'];
$sql.=', sum(hold='.$h.') as '.($h==0 ? '< 1 week' : '`> '.$h.' week'.($h>1? 's`' : '`');
}
$sql.=' from tablename group by product';
Parse error: syntax error, unexpected ';' in D:\QAFA\www\PE\Holding-test.php on line 5
Code
$rs=$db->query('select distinct hold from tablename order by hold');
$sql = "select hold";
while($ro=$rs->fetch_assoc()){
$h = $ro['hold'];
$sql.=', sum(hold='.$h.') as '.($h==0 ? '< 1 week' : '`> '.$h.' week'.($h>1? 's`' : '`');
}
$sql.=' from tablename group by product';