[英]Can SQL perform calculation on update?
Can SQL perform calculation on update? SQL可以对更新执行计算吗? For instance, the stored product quantity was 5 , and after being sold, say 3 items, I want to update the quantity left in the product tables
, 例如,存储的产品数量为5 ,在出售3件商品后,我想更新product tables
剩余的数量,
$sql_update = "
UPDATE store_products
SET
product_quantity = ?
WHERE store_products.product_id = ?
";
$result = $connection->run_query($sql_update,array(
3,
$product->product_id
));
Is it possible? 可能吗?
Yes - to achieve what you describe you need to implement an AFTER UPDATE
trigger. 是的-要实现您所描述的内容,您需要实现AFTER UPDATE
触发器。
For details and samples see: 有关详细信息和示例,请参见:
EDIT - as per comment a sample: 编辑-根据评论示例:
CREATE TRIGGER produpd AFTER UPDATE ON store_products
FOR EACH ROW BEGIN
UPDATE product SET quantity = quantity - NEW.product_quantity WHERE product_id = NEW.product_id;
END;
This trigger needs to be created in your MySQL DB... when you execute the SQL code from your question MySQL invokes this trigger and update the products
table. 当您从问题中执行SQL代码时,需要在MySQL数据库中创建此触发器。MySQL会调用此触发器并更新products
表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.