joehungjohn joehungjohn - 4 months ago 6
SQL Question

How to make sure to get sets of n rows in MySQL regardless of ID?

Right now I have the last ten rows using

SELECT col, col1, col2
FROM tabytle
ORDER BY ID DESC
limit 10


That works even with the gap of the latest ID having a value of 67 and the next entry having an ID value of 27.

With that in mind, if I were to hit "next" how would I get the next 10 rows which may not start with the ID of 18.

Should I count rows? And just loop/find rows till I get 10 unique IDs?

Answer

Use the offset parameter in limit

SELECT *
FROM tabytle 
ORDER BY ID DESC 
limit 10, 10

to get the next 10 records.