简体   繁体   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

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:现在我想弄清楚订单的总价放在哪里,我有两个想法:

  1. Add a 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和,或者:
  2. Add a 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:基本上,订单总额不一定与商品所有价格的总和相同:

  • The prices might change over time.价格可能会随着时间而变化。
  • The order itself might have discounts.订单本身可能有折扣。

In addition, the order might have additional charges:此外,订单可能会产生额外费用:

  • Discounts applied to the entire order.折扣适用于整个订单。
  • Taxes.税收。
  • Delivery 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.

相关问题 应该对order_products表进行非规范化吗? - Should order_products table be denormalized? MySQL订单表中订单项的摘要价格 - MySQL Summary price of order items in orders table MySQL:通过将位于两个单独表中的价格和数量列相乘来计算餐厅数据库中每个订单的总价 - MySQL: Calculating total price for each order in a restaurant database by multiplying the price and quantity columns located in two separate tables 使用不同表中的其他列 (pizza_price*pizza_amount) 更新订单表中的“总价格”列? - Update column "total price" in orders table using other columns (pizza_price*pizza_amount) in different tables? 结合多个mysql表中的“订单”并显示总价? - Combine multiple 'orders' from mysql table and display total price? MySQL中的产品和订单表结构 - Products & Orders Table Structure in MySQL mySQL WooCommerce-产品表,类别,价格和数量 - mySQL WooCommerce - Table of Products, Categories, Price and Quantity Sole 获取订单总价 - Get the total price of an order 如何使用mysql查询仅查询价格和promo_price之间价格比最大的产品 - How to order with mysql query only products which has biggest price ratio between price and promo_price 根据存储在两个不同表中的交易计算产品数量 - Products' quantity calculation based on transactions stored in two different tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM