SeeleyBoothe
SeeleyBoothe

Reputation: 261

MySQL Trigger With select

Ok i have a table called motors. When something is inserted in to that table i want a trigger to run that pulls back the last row added to the table, get the fields id, description, partnum and then insert those fields in to a different table called latest.

how would i write this?

thanks

Upvotes: 0

Views: 192

Answers (1)

Sam Dufel
Sam Dufel

Reputation: 17598

CREATE TRIGGER my_trigger AFTER INSERT ON motors
FOR EACH ROW
INSERT INTO latest (motor_id, motor_description, motor_partnum) 
  VALUES (NEW.id, NEW.description, NEW.partnum);

Although, I feel obligated to tell you that if you're just trying to generate a list of the most recent motors, it'll be easier to maintain if you just select the X newest motors from the motors table.

Upvotes: 2

Related Questions