Home  >  Article  >  Database  >  How to Retrieve the N-th Record from a MySQL Query?

How to Retrieve the N-th Record from a MySQL Query?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-17 15:31:02403browse

How to Retrieve the N-th Record from a MySQL Query?

Retrieving Specific Records from MySQL Queries

When working with large datasets, it may be necessary to retrieve a specific record based on its position in the result set rather than its unique identifier. This can be achieved using MySQL's LIMIT clause, which allows you to specify the number of records to return from a query starting at a certain offset.

To return the n-th record from a MySQL query (based on ascending ID order), you can use the following syntax:

SELECT * FROM table ORDER BY ID LIMIT n-1,1

Here's how this query works:

  • *: Selects all columns from the specified table.
  • ORDER BY ID: Sorts the results in ascending order by the ID column.
  • LIMIT n-1,1: Limits the result set to 1 record starting at record n-1. For example, LIMIT 2,1 will return the 3rd record.

The above is the detailed content of How to Retrieve the N-th Record from a MySQL Query?. 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