In database management, it often becomes necessary to retrieve the last row from a table. MySQL provides several methods to accomplish this task.
One common scenario arises when inserting data into a table and subsequently needing to retrieve a column value from the previous row. Notably, this table contains an auto-increment field.
To select the last row in such a table, consider the following query:
SELECT * FROM table_name ORDER BY id DESC LIMIT 1;
Here, id represents the auto-increment column. This query utilizes the ORDER BY clause to sort the rows in descending order based on the id column. The LIMIT 1 clause retrieves only the first row (the last row) from the sorted result set.
The above is the detailed content of How to Retrieve the Last Row from a MySQL Table with an Auto-Increment Column?. For more information, please follow other related articles on the PHP Chinese website!