繁体   English   中英

有订单和订单_产品表。订单总价应该存储在订单表中,还是根据产品数量和价格计算

[英]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

现在我想弄清楚订单的总价放在哪里,我有两个想法:

  1. Orders表中添加一个totalPrice列,该列将包含Orders中所有产品的price * quantity和,或者:
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM