Is there a way to get the N iteration of an SQL query ?
For example, if I want the second iteration :
Backup
id | id_device | nb_cut |
---|---|---|
11 | 222 | 853 |
10 | 5 | 698 |
9 | 222 | 589 |
8 | 5 | 123 |
7 | 222 | 456 |
… | … | … |
SELECT nb_cut FROM Backup WHERE id_device = 5 ORDER BY id DESC;
This query return 698. But I want the seconde iteration whose result would be 123.
2
Answers
Use LIMIT OFFSET
https://dbfiddle.uk/qOOw2LJl
The query shown above gets data starting the second row and limits the results to 1.
Literally the second result over a specific resultset filtered by id_device …. exists limit offset but this one could introduce you to a new world 😉
Look for "window functions" on the web for explanation