[英]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
表中是有关此人的所有信息,例如:
Example:例子:
In the orders
table is all the specific information about it such as:在
orders
表中是关于它的所有特定信息,例如:
id
field from the customers
table (customer_id)customers
表中的id
字段相关联 (customer_id) 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
链接的。
id
field from the orders
table (order_id)orders
表 (order_id) 中的id
字段相关联products
table.products
表中的 id 字段相关联。 (product_id) Example:例子:
In the products
table is all the information about the products:在
products
表中是关于产品的所有信息:
product_id
field from the order_items
table (id) order_items
表 (id) 中的product_id
字段相关联 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_price
每order_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_prices
的customers
要付钱?
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_item
的price
会很有用。 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.