当我需要显示总价为ORDER BY order_date的表时,我会显示错误的结果
我有一个数据库,其中:
- order_id
- order_date
- order_price
- order_quantity
我尝试过:
$sql = mysqli_query($con,"SELECT *,
SUM(order_q) * SUM(order_price) as total
FROM o_orders_f
GROUP BY order_id");
但是此代码将汇总所有结果并给出错误答案
$sql = mysqli_query($con, "SELECT *,
SUM(order_quantity) * SUM(order_price) as total
FROM o_orders_f
GROUP BY order_id");
while ($r = mysqli_fetch_array($sql)) {
$date = $r['order_date'];
$total = $r['total'];
echo $total;
}
我正在寻找的结果是这样的:
7 Jul, 2019 - total: ($r['quantity']* $r['price']) = 72933
8 Jul, 2019 - total: ($r['quantity']* $r['price']) = 453
9 Jul, 2019 - total: ($r['quantity']* $r['price']) = 72
答案 0 :(得分:0)
我在您的查询中看到2个错误:
order_date
分组时,您是按order_id分组的。SUM
进行运算,但是要求和它的各个因素。也就是说,您的查询应该是:
SELECT
*,
SUM(order_quantity * order_price) as total
FROM
o_orders_f
GROUP BY
order_date