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
Release: 2024-12-31 05:32:10
Original
1007 people have browsed it

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;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template