Latest few results separately fetched from database

Hello.

I know I can SELECT descending to get the latest results.

But if my records end at 155 how do I separately get 150, 151, 152, 153, 154 and 155?

For 155 I can say DESCENDING LIMIT 1 but next time when I want record 154 how do I get DESCENDING LIMIT 2 but only use record 154 and not use 155?

Thanks.

Maybe OFFSET ?
https://dev.mysql.com/doc/refman/5.7/en/select.html

use an offset:

limit 1 offset 2;
limit 1 offset 3;, …

the syntax for mysql defers.

1 Like

Do you mean to get the last x number of rows EXCEPT the last one where you don’t know the index/id/key of the last one? Only way I can think of doing it would be to sub-query it

This example is using the sample fiddle on SQLFiddle with a couple records added to the recordset

SELECT a.id
     , a.rev
     , a.content
FROM `docs` a
WHERE a.id < (SELECT MAX(id) FROM docs)
ORDER BY a.id desc
LIMIT 5

Thanks guys - chorn thanks for the coding.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.