Home >Database >Mysql Tutorial >How to Retrieve the Auto-Generated Primary Key After an INSERT in MySQL?
Retrieving Primary Key ID from MySQL INSERT Query
Question: How can I retrieve the newly generated primary key ID for an inserted record in the same query?
Answer: To obtain the primary key ID of the newly inserted record in MySQL, utilize the LAST_INSERT_ID() function. Syntax:
INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...); SELECT LAST_INSERT_ID();
This function retrieves the last generated primary key value for the current user connection. It provides:
The above is the detailed content of How to Retrieve the Auto-Generated Primary Key After an INSERT in MySQL?. For more information, please follow other related articles on the PHP Chinese website!