0

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. 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`);
4
  • 1
    It's probably a good idea to specify a column list. Commented Mar 21, 2013 at 18:20
  • Not sure what you mean...the columns are identical between the two tables, I just want to be able to grab the current date and time when the trigger occurs. Commented Mar 21, 2013 at 18:22
  • 1
    It doesn't matter. It's poor practice. What happens if the destination table changes column order or adds/removes a column? Commented Mar 21, 2013 at 18:23
  • I see what you're saying, and I'll make that change once I can figure out how to get the updated data that I need. Commented Mar 21, 2013 at 18:24

1 Answer 1

2

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`);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.