EN
MySQL - rows limit with offset
7 points
In this short article, we would like to show how to get rows from table using LIMIT
and OFFSET
in MySQL query.
LIMIT
and OFFSET
should be placed always as last.
Quick solution:
xxxxxxxxxx
1
SELECT *
2
FROM users
3
LIMIT 5 OFFSET 10;
Where: OFFSET
is counted starting from 0
(it means to get first row from table we need to use OFFSET 0
).
The same query can be written using shorter form:
xxxxxxxxxx
1
SELECT *
2
FROM users
3
LIMIT 10, 5;
Result:
- 5 users (LIMIT is 5),
- starting from 10th position (OFFSET is 10).
xxxxxxxxxx
1
SELECT u.*, m.*
2
FROM users u
3
JOIN messages m
4
ORDER BY m.id ASC
5
LIMIT 10, 5;