[英]Got orders and order_products tables.Should total price of order be stored in the orders table, or calculated based on the products quantity and price
所以我有以下3张表:
Table: Products
Columns: id, name, description, price, currency
Table: Orders
Columns: id, firstName, lastName, phoneNumber
Table: Order_Products
Columns: orderId, productId, quantity
现在我想弄清楚订单的总价放在哪里,我有两个想法:
Orders
表中添加一个totalPrice
列,该列将包含Orders
中所有产品的price * quantity
和,或者:Order_Products
表添加一个price
列,该列将包含该特定产品的price * quantity
,然后我必须获取该订单的所有Order_Products
记录并将其price
列相加。我不太确定哪个选项更好,因此我在这里征求建议。
我建议您将订单总额存储在orders
表中。
为什么? 基本上,订单总额不一定与商品所有价格的总和相同:
此外,订单可能会产生额外费用:
由于这些原因,我认为在下订单时将财务信息存储在订单上会更安全。
我不建议存储这个。 这是派生信息,可以在需要时即时计算。 如果您要经常进行计算,则可以使用视图:
create view orders_view as
select o.*, sum(p.price * op.quantity) total_price
from orders o
inner join order_products op on op.orderid = o.id
inner join products p on p.id = op.productid
group by o.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.