首先,我有一个名为store_00
的表id | ref | item | qty | cost | sell
1 22 x1 5 10 15
2 22 x2 10 5 10
3 23 x11 2 4 6
4 23 x22 1 5 10
5 24 x8 1 1 2
现在我希望输出如下:
| ref number 22 |
total cost = 100 | total sell = 175 | total qty = 15
| ref number 23 |
total cost = 13 | total sell = 22 | total qty = 3
依旧......
请注意我必须将每个成本相乘并卖给它的数量,因为上表显示单价...这就是让我的整个代码搞砸了,如果我这样做会更容易{ {1}}!但就我而言,结果会有所不同。
PHP代码:
SUM(qty), SUM(cost) , SUM(sell)
根据上面的代码,输出似乎只取每个分组引用的第一个值,如下所示:
$query2 = "SELECT * from store_00 GROUP by ref";
$result2 = mysql_query($query2);
While($row2 = mysql_fetch_assoc($result2)){
$ts = $row2['sell'] * $row2['qty'];
$ta = $row2['cost'] * $row2['qty'];
$sumts += $ts;
$sumta += $ta;
$sumqty += $row2['qty'];
echo "<table width=60% height=% align=center border=1 bgcolor=#FFFF99>";
echo "<tr><td align=center><font style='font-size:17px; font-weight:bold;'>TTL QTY = ".$sumqty."</font></td>";
echo "<td align=center><font style='font-size:17px; font-weight:bold;'>TTL Cost = ".number_format($sumta, 2, '.', '')." JD</font></td>";
echo "<td align=center><font style='font-size:17px; font-weight:bold;'>TTL Sell = ".number_format($sumts, 2, '.', '')." JD</font></td></tr>";
echo "</table>";
}
我不知道如何找到一种方法来乘以数值,然后将它们总结并分组参考,如果你有不同的方法,请分享它!
答案 0 :(得分:1)
可以在两列的产品上使用SUM。
SELECT
ref,
SUM(qty) as total_quantity,
SUM(qty*cost) as total_cost,
SUM(qty*sell) as total_sell
FROM `store_00`
GROUP BY ref
如果您使用此查询,则不需要在PHP中进行任何数学运算。