簡體   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