Retrieving the Last Row in a MySQL Table
How do you retrieve the last row in a MySQL table? This question arises when attempting to retrieve a specific column value from the preceding inserted row in a table containing an auto-incrementing primary key.
Solution Using MAX(id)
Despite having an auto-incrementing primary key, the most appropriate method to obtain the last row in the table is by utilizing the ORDER BY DESC LIMIT 1 clause:
SELECT fields FROM table ORDER BY id DESC LIMIT 1;
This query will fetch the last row based on the descending order of the id column.
The above is the detailed content of How to Retrieve the Last Row from a MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!