Search results
21 mar 2019 · You could store the last insert id in a variable : INSERT INTO table1 (title,userid) VALUES ('test', 1); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1); Or get the max id from table1 (EDIT: Warning.
The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted in a table. Syntax
Summary: in this tutorial, you will learn how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer successfully inserted for an AUTO_INCREMENT column.
Within the body of a stored routine (procedure or function) or a trigger, the value of LAST_INSERT_ID() changes the same way as for statements executed outside the body of these kinds of objects (see Section 14.15, “Information Functions”).
2 lut 2024 · There are 3 ways that we can use to get an ID of the latest inserted record in the MySQL table. All of these are listed below, and the table must have an AUTO_INCREMENT field to use any of the following approaches. Use the LAST_INSERT_ID() function. Use the max() function. Use the ORDER BY DESC clause.
Within the body of a stored routine (procedure or function) or a trigger, the value of LAST_INSERT_ID() changes the same way as for statements executed outside the body of these kinds of objects (see Section 12.15, “Information Functions”).
Get ID of The Last Inserted Record. If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately. In the table "MyGuests", the "id" column is an AUTO_INCREMENT field: