2014-03-05 166 views
1

這裏的問題是需要一些幫助PHP的MySQL

$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

感謝

+1

使用GROUP BY! https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html – Neels

+0

如果我添加GROUP BY product_id。它顯示了相同的結果 – Nazaret2005

+0

嘗試GROUP BY product_id,product_price – Neels

回答

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,則無法使用查詢來解決此問題。

+0

與試用 - 警告:mysqli_fetch_array()期望參數1爲mysqli_result,null在第90行給出的Z:\ home \ test2.ru \ www \ takeorders.php中。 – Nazaret2005

+0

忘記查詢,不會解決您的問題,請嘗試php – CodeBird

+0

如果我做像第二(用PHP)其播種我[代碼] 10:95 10:95 7:88 10:95 7:88 6:330 10:95 7:88 6:330 11 :64 [/ code] – Nazaret2005

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 
} 
+0

相同的結果:-( – Nazaret2005