MySQL: Saving the last inserted id using LAST_INSERT_ID()

This entry is part 19 of 26 in the series Learn MySQL

Another use for a variable is to save the result from LAST_INSERT_ID() after creating a new row in a table that has an AUTO_INCREMENT column.

INSERT INTO artist (artiste, album, price) VALUES ('Taylor Swift', 'Speak Now', 12.00);
SELECT @last_id := LAST_INSERT_ID();


Series Navigation<< MySQL: Assign expression to a variable
MySQL: Null variable >>