MYSQL get last inserted id
- User
MYSQL get last inserted id
- Assistant
To get the last inserted ID in MySQL, you can use the
LAST_INSERT_ID()function. Here's an example:sqlINSERT INTO your_table (column1, column2) VALUES ('value1', 'value2'); SELECT LAST_INSERT_ID();In this example, the
INSERT INTOstatement inserts a new row into "your_table" and theLAST_INSERT_ID()function retrieves the ID of the last inserted row. - User
And if my field is not named id ?
- Assistant
If your field is not named "id", you can still use the
LAST_INSERT_ID()function to retrieve the last inserted ID. The function returns the value of the auto-increment field for the most recently inserted row, regardless of the field name.Here's an example:
sqlINSERT INTO your_table (your_field) VALUES ('your_value'); SELECT LAST_INSERT_ID();Replace "your_table" with the name of your table, "your_field" with the name of your field, and "your_value" with the value you want to insert. The
LAST_INSERT_ID()function will return the ID of the last inserted row, regardless of the field name.