gpt4 book ai didi

mysql - 编写查询以获取两列相乘的总和

转载 作者:行者123 更新时间:2023-11-29 20:48:33 25 4
gpt4 key购买 nike

我编写了查询来获取两列的乘法之和。

select *,sum(op.qty*op.price) as total from `order` as o 
join order_products as op on op.order_id = o.order_id
where o.user_id = 5 and o.order_id = 10 group by op.order_product_id

我的输出看起来像 enter image description here

在这里,如图所示,我想要所有记录以及总数。

那么,我应该做什么?

最佳答案

我已将查询更改为

select
t1.*,
t2.total
FROM
(
SELECT op.*
FROM `order` AS o JOIN order_products AS op ON op.order_id = o.order_id
WHERE o.user_id = 5 AND o.order_id = 10
) as t1
LEFT JOIN
(
SELECT sum(op.qty * op.price) as total
FROM `order` AS o JOIN order_products AS op ON op.order_id = o.order_id
WHERE o.user_id = 5 AND o.order_id = 10
) as t2
ON 1 = 1

它给出的结果是这样的

enter image description here

这个查询运行完美。

关于mysql - 编写查询以获取两列相乘的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243385/

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