As we all know, the MySQL LAST_INSERT_ID() function returns the latest generated sequence number, but in the case of multi-row insertion, it will return the row inserted by the most front Generated serial number.
mysql> Insert into Student(Name) values('Ram'),('Mohan'),('Aryan'); Query OK, 3 rows affected (0.03 sec) Records: 3 Duplicates: 0 Warnings: 0
The above query inserts three values into the Student table with the help of a multi-row insert query. The value of the column "Id" can be checked with the help of the following query -
mysql> Select * from Student; +----+-------+ | Id | Name | +----+-------+ | 1 | Raman | | 2 | Rahul | | 3 | Ram | | 4 | Mohan | | 5 | Aryan | +----+-------+ 5 rows in set (0.00 sec)
This means that Last_Insert_Id() must return 5 as output, but we can see that it returns the value 3 as follows -
mysql> Select Last_Insert_Id(); +------------------+ | Last_Insert_Id() | +------------------+ | 3 | +------------------+ 1 row in set (0.00 sec)
It returns the value 3 because 3 is the value of the first inserted row in the above multi-row insert query.
The above is the detailed content of In the case of multiple row insertions, what is the impact on the output of the MySQL LAST_INSERT_ID() function?. For more information, please follow other related articles on the PHP Chinese website!