when inserting an entry into MySQL, you sometimes want to get the ID of the inserted entry, one way is to execute a SELECT statement condition for MAX (ID) to get, but this form is not thread-safe in the concurrency environment, Because there may already be another entry inserted between your completion insert and the maximum ID that is executed again to perform a select.
A thread-safe solution is to use the Select LAST_INSERT_ID () statement, which returns the ID of the first inserted entry in this link (each database link is assumed by a thread)
eg.
(1), in connection 1 to a table insert a record, a table contains an ID of auto_increment type.
(2), in connection 2 to a table and then insert a record.
(3), result: The result of executing select last_insert_id () in connection 1 is different from the result of executing select last_insert_id () in connection 2, and the result of executing select MAX (ID) in two connections is the same.
Also, when using select LAST_INSERT_ID (), be aware that when inserting multiple records at one time, just get the first inserted ID value, be careful!
Thread safely gets the ID of the entry that was inserted into MySQL