MySQL Equivalent of SQL Server's SCOPE_IDENTITY()
When working with auto-incrementing columns in MySQL, you may encounter a need to retrieve the ID of the last inserted record, analogous to the SCOPE_IDENTITY() function in SQL Server. This article explores the equivalent in MySQL and provides detailed examples for its usage.
Equivalent of SCOPE_IDENTITY() in MySQL
The equivalent function in MySQL is LAST_INSERT_ID(). This function returns the last auto-incrementally generated value for the most recent insert operation executed within the current connection.
Usage Example
Consider the following MySQL code:
INSERT INTO Customers (Name, Email) VALUES ('John Doe', 'john.doe@example.com'); SELECT LAST_INSERT_ID();
The first statement inserts a new record into the Customers table and assigns an auto-incrementing ID to the newly added row. The second statement retrieves the ID of the last inserted record using the LAST_INSERT_ID() function.
Additional Notes
The above is the detailed content of What\'s the MySQL Equivalent of SQL Server\'s SCOPE_IDENTITY()?. For more information, please follow other related articles on the PHP Chinese website!