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