我有一个带两张桌子的餐馆数据库。其中一个表是'item'
,具有以下属性:
ItemID, ItemName, Price and Cuisine.
我有另一个表'bill'
,它具有以下属性:
OrderID, Item1_ID, Item2_ID, Item3_ID, Quantity_1, Quantity_2, Quantity_3, Total.
bill的第2,第3和第4个属性指定3个具有不同ItemID的项目,第5个,第6个和第7个属性指定它们各自的数量。我想通过将每个项目的价格乘以它们的价格(从“项目”表中获取)计算每个项目的价格,然后添加它们各自的价格并将结果保存在总数中。
1.我能够生成三个单独的价格,但由于它们是派生属性,我无法保存它们,因此更新了“账单”的“总计”属性。我该怎么做呢?这是我用来生成三个单独价格的代码:
SELECT A.Total_1, B.Total_2, C.Total_3
FROM
(SELECT bill.orderid, bill.quantity_1 * item.Price AS 'Total_1'
FROM bill, item
WHERE item.itemid = bill.item_1
)A,
(SELECT bill.orderid, bill.quantity_2 * item.Price AS 'Total_2'
FROM bill, item
WHERE item.itemid = bill.item_2
)B,
(SELECT bill.orderid, bill.quantity_3 * item.Price AS 'Total_3'
FROM bill, item
WHERE item.itemid = bill.item_3
)C
WHERE A.orderid = B.orderid AND A.orderid = C.orderid;
答案 0 :(得分:0)
你可以像这样使用somting:
select
item_1 * price_1 + item_2 * price_2 + item_3 * price_3 as total
item_1 * price_1 as total_1
item_2 * price_2 as total_2
item_3 * price_3 as total_3
where orderId = yourOrderId
如果你在你的选择(+,*,/, - )中进行逻辑操作,就像你的选择快速分开你的项目并用你喜欢的语言进行操作。
答案 1 :(得分:0)
快速简便的解决方案是将现有查询用作派生表并从中进行选择:
SELECT Total_1, Total_2, Total_3,
Total_1 + Total_2 + Total_3 AS GrandTotal
FROM (
SELECT A.Total_1, B.Total_2, C.Total_3
FROM
(SELECT bill.orderid, bill.quantity_1 * item.Price AS 'Total_1'
FROM bill, item
WHERE item.itemid = bill.item_1
)A,
(SELECT bill.orderid, bill.quantity_2 * item.Price AS 'Total_2'
FROM bill, item
WHERE item.itemid = bill.item_2
)B,
(SELECT bill.orderid, bill.quantity_3 * item.Price AS 'Total_3'
FROM bill, item
WHERE item.itemid = bill.item_3
)C
WHERE A.orderid = B.orderid AND A.orderid = C.orderid
) T;
编辑:
要将GrandTotal
存储在表Total
的{{1}}列中,您可以这样做:
Bill