Mysql obtains the insert ID. in MySQL, The auto_increment type id field is used as the primary key of the table. The general practice is to use the "select max (id) from tablename" method, but it is clear that this method requires consideration of concurrency and requires the "X lock" on the master table in the transaction, unlock after obtaining the value of max (id. Www.2cto.com requires a lot of steps, which is troublesome and has poor concurrency. Is there a simpler way? One of the answers is through the select LAST_INSERT_ID () operation. At first glance, it is very similar to select max (id), but in fact it is thread-safe. That is to say, it is based on database connection. What is the significance of database-based connection? Example: www.2cto.com (1). Insert A record to Table A in connection 1. Table A contains an auto_increment id. (2) Insert another record to Table A in connection 2. (3) result: the result obtained by executing select LAST_INSERT_ID () in connection 1 is different from the result of executing select LAST_INSERT_ID () in connection 2; the results of executing select max (id) in the two connections are the same. Note: When Using select last_insert_id (), note that when multiple records are inserted at a time, only the id value inserted for the first time is obtained!