0
我想使用以前訂單中的信息製作「暢銷品」清單。我現在擁有的是這樣的東西;如何製作暢銷品清單?
Product Quantity
2227 30
1722 3
1851 7
2227 10
1722 4
1863 1
etc....
第一列(產品)是數據庫中每個產品的唯一ID。數量當然有多少項目已經售出。每行是一個訂單。因此ID 2227
在此列表中出現兩次。
我該如何對這些數據進行排序,以便共獲得ID 2227
的銷售量?
我此刻的PHP是:
$SQL_best = "SELECT c.company, co.id, cod.productId, cod.quantity
FROM customers c
LEFT JOIN customers_orders co ON c.id = co.custId
LEFT JOIN customers_orders_details cod ON co.id = cod.orderId
WHERE c.reseller =1
AND c.status != 99
AND c.id = ".$intCustomerId;
$result_best = $objDB->sqlExecute($SQL_best);
*some html code here*
<table style="margin:0px auto;">
<tr>
<th>Product</th>
<th>Quantity</th>
</tr>
<?php
while($obj_best = $objDB->getObject($result_best)) {
if ($obj_best->quantity > 0) { // don't include negatve quantaties (RMA's/refunds)
echo "<tr>";
echo "<td>".$obj_best->productId."</td>";
echo "<td>".$obj_best->quantity."</td>";
echo "</tr>";
}
}
?>
</table>
MySQL查詢
所以我需要加在一起的所有$obj_best->productId
的。在這種情況下我該怎麼做?或者我應該編輯我的查詢?
添加'SUM(cod.quantity)'到您的選擇列表;然後按該列排序desc ....篩選出SQL查詢中的負數,而不是PHP –
您應該更改查詢。您需要產品上帶有GROUP BY子句的聚合函數SUM – AgRizzo