如何获取触发器的插入值 [英] How to get inserted value for trigger
问题描述
我正在尝试在mysql中创建一个触发器,该触发器将在插入表时插入到另一个表中.
I am trying to create a trigger in mysql that will insert in to another table when an insert on a table occurs.
CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
BEGIN
INSERT INTO swiped.Card (userid) VALUES (get value from original insert here);
END
在insert语句的values部分中,我如何从原始insert中获取一个值以在此处使用?
In the values part of the insert statement how would i get a value from the original insert to use here?
谢谢
推荐答案
您可以将这些值与new.columnname
一起使用.如果该列的名称也为userid
,则可以使用:
You can use those values with new.columnname
. If the name of the column is userid
too, then you can use:
CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
BEGIN
INSERT INTO swiped.Card (userid) VALUES (new.userid);
END
从官方文档中:
在触发器主体内,OLD和NEW关键字使您可以访问 受触发器影响的行中的列.旧的和新的是MySQL 触发器的扩展;它们不区分大小写.
Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.
在INSERT触发器中,只能使用NEW.col_name
In an INSERT trigger, only NEW.col_name can be used
这篇关于如何获取触发器的插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!