2

I want to insert the last updated row into a new table using a trigger?

How to fetch that row if any of the column (value) from that table has been updated or changed?

I am using this code:

CREATE TRIGGER database.tbl1_AFTER_UPDATE 
    AFTER UPDATE ON tbl1 
FOR EACH ROW 
BEGIN 
    INSERT INTO tbl2 (c1,c2,c3) 
    SELECT c1,c2,c3 
    from tbl1; 
END 

But this is inserting all the rows from tbl1 into tbl2. I want only the updated rows to be inserted into tbl2.

How can I do it?

0

1 Answer 1

3

From http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html:

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.

So if you want the new rows being inserted on another table, you should use the syntax:

INSERT INTO tbl2 (c1, c2, c3) VALUES (NEW.c1, NEW.c2, NEW.c3);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.