Home >Database >Mysql Tutorial >How to Retrieve the Last Row from a MySQL Table with an Auto-Increment Column?

How to Retrieve the Last Row from a MySQL Table with an Auto-Increment Column?

Linda Hamilton
Linda HamiltonOriginal
2024-12-10 19:01:09391browse

How to Retrieve the Last Row from a MySQL Table with an Auto-Increment Column?

Retrieve Last Row from MySQL Table with Auto_Increment

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!

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