Soroosh
Soroosh

Reputation: 755

How to query first 5 rows of a mysql table

I know that I can query a range of ids using BETWEEN command but there is a situation that the id of first 5 rows are like this: 1,5,6,7,8

In this situation if I query using BETWEEN command I will get 1,5.

Is there any way to achieve this?

Upvotes: 0

Views: 4748

Answers (1)

Bohemian
Bohemian

Reputation: 425033

Use order by and limit:

select * from mytable
order by id
limit 5

You need order by to give an order to the rows. Without order by, rows have no defined order.

——

To return 5 rows starting from the nth row:

...
limit n, 5

Upvotes: 2

Related Questions