cuốn sách gpt4 ai đã làm

具有相同引用编号的 PHP 求和

In lại Tác giả: Walker 123 更新时间:2023-11-28 23:34:09 29 4
mua khóa gpt4 Nike

首先,我有一个名为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

vân vân……

请注意我必须将每个成本乘以它的数量因为上表显示的是单价...这就是让我的整个代码搞砸的原因,如果我这样做会更容易SUM(qty), SUM(cost), SUM(sell) !但就我而言,结果会有所不同。

PHP 代码:

$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 "";

echo "";
echo "";
echo "";
echo "
TTL QTY = ".$sumqty."TTL Cost = ".number_format($sumta, 2, '.', '')." JDTTL Sell = ".number_format($sumts, 2, '.', '')." JD
";

}

根据上面的代码,输出似乎只从每个分组的 ref 中获取第一个值,如下所示:

     | ref number 22 |
total cost = 50 | total sell = 75 | total qty = 5

我不知道如何找到一种方法来乘以值,然后将它们相加并一次对所有引用进行分组,如果您有不同的方法,请分享!

câu trả lời hay nhất

可以对两列的乘积使用 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 中进行任何数学计算。

关于具有相同引用编号的 PHP 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295948/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress