[英]MySQL - Insert row into table 2 for each updated row in table 1
I have a table_1
that looks like this: 我有一个
table_1
看起来像这样:
serial myBool dateTime
------ ------ --------
1 0 2016-05-03 10:23:45
2 1 2016-05-03 09:13:21
3 0 2016-05-03 08:44:33
A MySQL scheduled event runs periodically and if the dateTime is old enough, it sets myBool
to 0. So far so good. MySQL计划的事件定期运行,并且如果dateTime足够旧,它将
myBool
设置为0。 Each time myBool
is set to 0, I wish to add a new row to table_2
as follows: 每次将
myBool
设置为0时,我都希望向table_2
添加一个新行,如下所示:
table_1_serial dateTime
-------------- --------
2 2016-05-03 09:13:21
I created a trigger thus: 我这样创建了一个触发器:
CREATE TRIGGER `myTrigger` AFTER UPDATE ON `table_1`
FOR EACH ROW
IF myBool = 0 THEN INSERT INTO `table_2` VALUES(`serial`, `dateTime`);
END IF
However now when I modify a table_1
row, I get this message: 但是现在,当我修改一个
table_1
行时,会收到以下消息:
Unknown column 'myBool' in 'field list'
I'd appreciate any help getting the trigger working, thanks! 感谢您为触发器工作提供的任何帮助,谢谢!
Try using new.
尝试使用
new.
for the column reference: 供列参考:
CREATE TRIGGER `myTrigger` AFTER UPDATE ON `table_1`
FOR EACH ROW
IF new.myBool = 0 THEN
INSERT INTO `table_2`
VALUES(new.`serial`, new.`dateTime`);
END IF;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.