gpt4 book ai didi

MySQL 连接两个表并求和?

转载 作者:行者123 更新时间:2023-11-29 10:08:32 25 4
gpt4 key购买 nike

enter image description here

我有 2 个表 - ordersorderdetail

我需要将它们连接在一起并显示总数。我可以加入他们,但我如何计算总数?

SELECT * 
FROM Orders as o
INNER JOIN OrderDetails as od on o.order_id = od.order_id
WHERE o.table_id = 1

最佳答案

 select a.order_id,
order_date,
order_status,
table_id,
item_id,
item_price,
quantity,
b.item_price*b.quantity as total
from dbo.orders as a inner join dbo.orderdetails as b
on a.order_id=b.order_id
WHERE table_id = 1

Update : as you mentioned in comment if you want a computed column which automatically multiply quantity and item_price, then you need execute this query :

  ALTER TABLE dbo.orderdetails ADD Total AS (quantity* item_price);  

关于MySQL 连接两个表并求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51431674/

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