MYSQL更新触发器获取更新的行ID [英] MYSQL Update Trigger get updated row ID
问题描述
我在MySQL中有一个要更新的表。我希望触发器获取更新的行ID,以便对其进行一些处理。我该怎么做?我可以在SQL Server中执行此操作,但是我是MySQL的新手,无法在线找到任何内容。
I have a table in MySQL which I am updating. I would like the trigger to get the updated row ID so as to do some work with it. How can I do it? I can do this in SQL Server, but I am new to MySQL and couldn't find anything online.
更新...
所以我不能共享代码,但这基本上是我想做的。
So I can't share the code, but this is basically want I want to do.
Table:
**id** **product** **price**
1, Coke, 2.05 USD
2, Sprite, 2.00 USD
3, 7Up, 2.10 USD
我想将id 2 Sprite价格更新为2.50 USD。现在,我将正常执行此操作,但是我需要运行触发器来更新审核表。审核表将需要注册产品的ID。所以基本上我需要知道更新的行的id是2 ...
I want to update id 2, "Sprite" price to 2.50 USD. Now I will do this normally, but I need to run a Trigger that will update an Audit table. The Audit table will need to register the id of the product. So basically I need to know that the updated row's id was 2...
sql中是否有类似内容?
is there anything similar in sql?
推荐答案
您可以使用 NEW.id
id 列>或 OLD.id
,只要 id
列不变。
You can refer to the id
column using either NEW.id
or OLD.id
as long as the id
column will not change.
通常所说,使用别名 NEW
可以获取更新列的当前值,并使用 OLD
您将获得该列在执行更新前所具有的值。
Generally spoken, with the alias NEW
you can get the current value of the updated column and with OLD
you obtain the value that the column had before the update was performed.
CREATE TRIGGER au_product_table
AFTER UPDATE ON product_table
FOR EACH ROW
UPDATE audit
SET product_id = NEW.id
WHERE your_condition;
这篇关于MYSQL更新触发器获取更新的行ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!