简体   繁体   English

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

[英]Insert row into another table via mySQL trigger

I'm getting a syntax error when trying to create a trigger to insert a row from one table into another...updating the date and time in the process. 我在尝试创建触发器以将一行从一个表插入另一个表时出现语法错误...更新过程中的日期和时间。 Here's my query: 这是我的查询:

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`);

This trigger will run when first_table is updated. 更新first_table时将运行此触发器。 I'm not sure if this is the correct way to accomplish the task. 我不确定这是否是完成任务的正确方法。 I've generalized the field and table names for simplicity. 为简单起见,我概括了字段和表名。

EDIT: 编辑:

The columns are identical between the two tables. 两个表之间的列是相同的。 I just want the ability to update the date and time when the trigger occurs. 我只想更新触发发生时的日期和时间。 This works, but obviously just copies the previous date and time: 这有效,但显然只复制以前的日期和时间:

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

Try: 尝试:

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`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM