简体   繁体   English

MySQL插入数据时自动更新字段

[英]MySQL automatically updating field when inserting data

I'm making a web application to make customers order items for anything.我正在制作一个网络应用程序,让客户订购任何东西。 For that I've made a MySQL database which has the following tables:为此,我制作了一个 MySQL 数据库,其中包含以下表格:

  • customers
  • orders
  • orders-items
  • products

In the customers table is all the information about the person such as:customers表中是有关此人的所有信息,例如:

  • The customer ID, for the primary key and auto increment (id)客户 ID,用于主键和自动增量 (id)
  • The first name (first_name)名字 (first_name)
  • The last name (last_name)姓氏 (last_name)
  • The email address (email_address)电子邮件地址 (email_address)
  • Information about the customer (customer_info)有关客户的信息 (customer_info)

Example:例子:

在此处输入图片说明

In the orders table is all the specific information about it such as:orders表中是关于它的所有特定信息,例如:

  • The order ID, for the primary key and auto increment (id)订单 ID,用于主键和自动增量 (id)
  • Which customer it ordered, linked with id field from the customers table (customer_id)它订购了哪个客户,与customers表中的id字段相关联 (customer_id)
  • Order information (order_info)订单信息(order_info)
  • The location where the order needs to go to (location)订单需要去的位置(位置)
  • The total price the customer has to pay (total_price)客户必须支付的总价 (total_price)
  • When the order was created (created)订单创建时间(已创建)

Example:例子:

在此处输入图片说明

In the orders-items table are all the items which every customer ordered, this is being linked by the order-id from the previous table.orders-items表中是每个客户订购的所有项目,这是由上一个表中的order-id链接的。

  • The ID, for primary key and auto increment, not used for any relation (id) ID,用于主键和自增,不用于任何关系 (id)
  • The order ID, used for which product is for which order.订单 ID,用于哪个产品对应哪个订单。 This is linked with the id field from the orders table (order_id)这与orders表 (order_id) 中的id字段相关联
  • The product ID, this is used for what product they ordered, this is linked with the id field from the products table.产品 ID,用于他们订购的产品,这与products表中的 id 字段相关联。 (product_id) (product_id)
  • The amount of this product they ordered (quantity)他们订购的这个产品的数量(数量)

Example:例子:

在此处输入图片说明

In the products table is all the information about the products:products表中是关于产品的所有信息:

  • The ID, for primary key and auto incrementing, This is linked with the product_id field from the order_items table (id) ID,用于主键和自动递增,这与order_items表 (id) 中的product_id字段相关联
  • The name of the product (name)产品名称(名称)
  • The description of the product (description)产品描述(描述)
  • The price of the product (price)产品的价格(价格)

Example:例子:

在此处输入图片说明

Question:题:

I've got this query:我有这个查询:

SELECT `orders-items`.`order_id` , SUM(`orders-items`.`quantity`* `products`.`price`) total
FROM  `orders-items` 
INNER JOIN  `Products` ON  `orders-items`.`products_id` =  `products`.`id` 

And it shows me a list of all the total prices every order_id has to pay.它向我显示了每个order_id必须支付的所有总价的列表。

But how do I make this so that this value of the total_price every order_id has to pay is automatticly inserted into the orders table inside the total_price field at the right order_id when inserting a product into my orders-list table?但我怎么做这使得这个值total_priceorder_id已支付的automatticly插入orders的表里面total_price在右场order_id插入产品进入我当orders-list表?

Or is it still better to not keep track of the total_prices the customers have to pay?或者是它最好还是不跟踪的total_pricescustomers要付钱?

A couple things to consider.需要考虑的几件事。

Having a total_price for itself is redundant.为自己设置total_price是多余的。 You can learn this total by summing the prices of this order's items at any time.您可以随时通过汇总此订单商品的价格来了解此总数。 It might be interesting to have it for performance reasons, but is this really necessary for your scenario?出于性能原因使用它可能会很有趣,但这对于您的场景真的有必要吗? It rarely is.它很少是。

Having a price on each order_item in the other hand would be useful.另一方面,每个order_itemprice会很有用。 And the why is because thoses products prices might change in the future and you don't want to lose information of for how much they were sold at the time of that particular sale.原因是因为这些产品的价格在未来可能会发生变化,并且您不想丢失在该特定销售时销售了多少的信息。

In any case, you can update your total_price using triggers like this:在任何情况下,您都可以使用以下触发器更新您的total_price

DELIMITER $$

CREATE TRIGGER order_items_insert AFTER INSERT ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

CREATE TRIGGER order_items_update AFTER UPDATE ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

CREATE TRIGGER order_items_delete AFTER DELETE ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = old.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

DELIMITER ;

One Option would be a trigger , which is executed on inserting a new customer.一个选项是trigger ,它在插入新客户时执行。

Alternative you could going for a stored procedure .您可以选择使用stored procedure With this, you only need to call the procedure InsertCustomer , and the database handles the price update for you -> you will not have these dependencies in your Application.有了这个,您只需要调用过程InsertCustomer ,数据库就会为您处理价格更新 -> 您的应用程序中将没有这些依赖项。

Another way could be, to do 2 querys (insert and update) in a transaction .另一种方法是,在一个transaction执行 2 个查询(插入和更新)。 But for this i recommend Domain Driven Design .但为此我推荐Domain Driven Design You would have a service, which has the method CreateCustomer(Customer $customer) , which do a insert query and then the update query.你会有一个服务,它有方法CreateCustomer(Customer $customer) ,它执行插入查询,然后执行更新查询。 On success it commits the transaction and returns true, if not success, it cancel the transaction and reuturns false.如果成功,它提交事务并返回真,如果不成功,它取消事务并返回假。 It should be your own convention to only manipulate data with help of the services (which knows the business logic).仅在服务(知道业务逻辑)的帮助下操作数据应该是您自己的惯例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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