Skip to content
Advertisement

Populating a recordset between two positions in a table using MYSQL

I’m trying to write a query that will only return a recordset between 2 positions in a table

enter image description here

For example, using Excel to demonstrate, populate a recordset between positions 3 and 7

I thought about using BETWEEN with the ID field, but there may be times that a record has been deleted and the ID field auto-incrementation will be out.

Ultimately, I want to use the query to populate a list for reporting:

Page 1 shows Records 1 to 10
Page 2 shows Records 11 to 20
Page 3 shows Records 21 to 30 etc

Advertisement

Answer

Why not use limit and offset?

select t.*
from t
order by id
limit 10 offset 0;

Then for subsequent pages, you would have:

limit 10 offset 9;
limit 10 offset 19;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement