gpt4 book ai didi

php - 分组后的mysql总和

转载 作者:行者123 更新时间:2023-11-29 01:51:19 27 4
gpt4 key购买 nike

购物车表:

itemid | amount 
------ | ------
1 | 2
2 | 4

产品表:

itemid | Price
------ | ------
1 | 10
2 | 20

到目前为止我已经尝试过:

$cartPrice = 
"SELECT (SUM(products.Price) * SUM(cart.amount))
AS total_price
FROM cart
INNER JOIN products
ON cart.itemid = products.itemid
WHERE userid = '$IP'
GROUP BY cart.itemid";

$Price = $mysqli->query($cartPrice);
$rowPrice = $Price->fetch_assoc();

echo "Total: ". $rowPrice['total_price'] ." ";

我的 MYSQL 查询输出如下:

total_price |
----------- |
20 |
80 |

它将回显最后的结果。但是我需要总数为 80 + 20。我该怎么做?

我现在得到的结果:

Total: 80

我想要的结果:

Total: 100

谢谢

最佳答案

您不必 GROUP BY 因为您只想返回一行:

SELECT SUM(products.Price* cart.amount) AS total_price 
FROM cart
INNER JOIN products
ON cart.itemid = products.itemid
WHERE userid = '$IP'

关于php - 分组后的mysql总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41897817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com