[英]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
So I have the following 3 tables:所以我有以下3张表:
Table: Products
Columns: id, name, description, price, currency
Table: Orders
Columns: id, firstName, lastName, phoneNumber
Table: Order_Products
Columns: orderId, productId, quantity
Now I'm trying to figure out where to put the total price of the order and I have 2 ideas:现在我想弄清楚订单的总价放在哪里,我有两个想法:
totalPrice
column to the Orders
table that will contain the sum of the price * quantity
of all products in the order, or:在Orders
表中添加一个totalPrice
列,该列将包含Orders
中所有产品的price * quantity
和,或者:price
column to the Order_Products
table that will contain the the price * quantity
of that specific product, and then I'd have to get all Order_Products
records for that order and sum their price
columns.向Order_Products
表添加一个price
列,该列将包含该特定产品的price * quantity
,然后我必须获取该订单的所有Order_Products
记录并将其price
列相加。I'm not quite sure which option is better, hence why I'm asking for recommendations here.我不太确定哪个选项更好,因此我在这里征求建议。
I would recommend that you store the order total in the orders
table.我建议您将订单总额存储在orders
表中。
Why?为什么? Basically, order totals are not necessarily the same as the sum of all the prices on the items:基本上,订单总额不一定与商品所有价格的总和相同:
In addition, the order might have additional charges:此外,订单可能会产生额外费用:
For these reasons, I think it is safer to store financial information on the order when the order is placed.由于这些原因,我认为在下订单时将财务信息存储在订单上会更安全。
I woulnd't recommend storing this.我不建议存储这个。 This is derived information, that can be computed on the fly whenever needed.这是派生信息,可以在需要时即时计算。 If you are going to do the computation often, you can use a view:如果您要经常进行计算,则可以使用视图:
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.