Home  >  Article  >  Database  >  How Does MySQL\'s `LIMIT` with `OFFSET` Determine the Range of Retrieved Rows?

How Does MySQL\'s `LIMIT` with `OFFSET` Determine the Range of Retrieved Rows?

Barbara Streisand
Barbara StreisandOriginal
2024-11-23 22:00:12432browse

How Does MySQL's `LIMIT` with `OFFSET` Determine the Range of Retrieved Rows?

LIMIT with OFFSET in MySQL: Understanding Query Results

The combination of LIMIT and OFFSET clauses in MySQL allows for the selection of a specific subset of rows from a table. This technique is useful for pagination and retrieval of specific data segments.

Query:

Consider the following query:

SELECT column 
FROM table
LIMIT 18 OFFSET 8

Number and Range of Results:

This query will return 18 rows as output, starting from record #9 and ending at record #26. To determine this range, follow these steps:

  1. Offset by 8: The OFFSET clause specifies that the initial 8 records should be skipped before starting the retrieval.
  2. LIMIT by 18: The LIMIT clause limits the number of rows to be retrieved to 18.
  3. Result Range: By combining OFFSET and LIMIT, the query retrieves 18 records from record #9 (9 8) to record #26 (9 8 17).

Example:

Let's say we have a table with the following data:

id column
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
11 K
12 L
13 M
14 N
15 O
16 P
17 Q
18 R
19 S
20 T

The query would return the following result:

column
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

The above is the detailed content of How Does MySQL\'s `LIMIT` with `OFFSET` Determine the Range of Retrieved Rows?. 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