通过mySQL触发器将行插入另一个表

时间:2013-03-21 18:19:25

标签: mysql triggers

尝试创建触发器以将一行从一个表插入另一个表时,我收到语法错误...更新过程中的日期和时间。这是我的疑问:

INSERT INTO `second_table` (field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11, field12)
  (SELECT `field1`, `field2`, `field3`, `field4`, `field5`, `field6`, `field7`, `field8`, `field9` FROM `first_table` WHERE `field1` = NEW.`field1`), CURDATE(), NOW(), (SELECT `field12` FROM `second_table` WHERE `field1` = NEW.`field1`);

此更新将在first_table更新时运行。我不确定这是否是完成任务的正确方法。为简单起见,我概括了字段和表名。

编辑:

两个表之间的列是相同的。我只想更新触发发生时的日期和时间。这有效,但显然只是复制了以前的日期和时间:

INSERT INTO `second_table` (SELECT * FROM `first_table` WHERE `field1` = NEW.`field1`);

1 个答案:

答案 0 :(得分:2)

尝试:

INSERT INTO `second_table` (field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11, field12)
 (SELECT `field1`, `field2`, `field3`, `field4`, `field5`, `field6`, `field7`, `field8`, `field9`, CURDATE() AS `field10`, NOW() AS `field11`, `field12` FROM `first_table` WHERE `field1` = NEW.`field1`);