Home >Database >Mysql Tutorial >How to Retrieve the Last Row from a MySQL Table?

How to Retrieve the Last Row from a MySQL Table?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-31 05:32:101005browse

How to Retrieve the Last Row from a MySQL Table?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn