user1638466
user1638466

Reputation: 310

MySQL trigger issue with if clause

I'have a problem trying to create a trigger. I have table_1 and table_2, the update event on table_1 should update a row in table_2 identified using the where clause:

CREATE DEFINER=`root`@`localhost` TRIGGER `update_table_2` AFTER UPDATE ON `table_1` FOR EACH ROW BEGIN

   IF table_1.key=0 THEN
      UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';   
   END IF;

END

What's my problem?! Thanks

Upvotes: 1

Views: 51

Answers (1)

Filipe Silva
Filipe Silva

Reputation: 21657

In a trigger, you should use the old and new keywords to reference the record that has been updated (the old values and new ones respectively).

In your case, you should do :

   IF new.key=0 THEN
      UPDATE table_2 SET table_2.value='10' WHERE table_2.key='2';   
   END IF;

Upvotes: 1

Related Questions