需要一点帮助php mysql

时间:2014-03-05 10:54:31

标签: php mysql loops while-loop sum

这是问题

$rd = do_mysql_query("SELECT sum(product_price * '{$count}') as letsee 
                      FROM prices 
                      WHERE product_id = '{$record['prodid']}' 
                      AND id='{$id_p}' 
                      ORDER BY product_id") or sqlerr(__FILE__, __LINE__);
while($row = mysqli_fetch_array($rd)){
echo "{$record['prodid']}:{$row['letsee']}<br />"; //id:count
}

它出来给我

10:130
10:95
7:88
6:35
6:120
6:330
11:104
11:64

我需要通过id

加上所有计数

需要的结果

  • 10:225
  • 7:88
  • 6:485
  • 11:168

感谢

2 个答案:

答案 0 :(得分:3)

你可以用php:

来做
foreach($records as $record){//First loop or something like it
    $rd = do_mysql_query("SELECT sum(product_price * '{$count}') as letsee FROM prices WHERE product_id = '{$record['prodid']}' AND id='{$id_p}' ORDER BY product_id") or sqlerr(__FILE__, __LINE__);
    while($row = mysqli_fetch_array($rd)){
        $final_array[$record['prodid']]+=$row['letsee']; //id:count
    }
}//End of First loop or something like it

foreach($final_array as $key => $value){
     echo $key.':'.$value;
}

注意:我认为你应该使用php,因为看起来检查你的代码,你有另一个循环得到$record['prodid']$count导致重复。如果您多次发送相同的product_id,则无法通过查询解决此问题。

答案 1 :(得分:0)

试试这个

$rd = do_mysql_query("SELECT sum(product_price) as letsee FROM prices WHERE product_id = '{$record['prodid']}' AND id='{$id_p}' GROUP BY product_id ORDER BY product_id") or sqlerr(__FILE__, __LINE__);
while($row = mysqli_fetch_array($rd)){
echo "{$record['prodid']}:" . $row['letsee'] * $count ."<br />"; //id:count
}