For a given ID, I'm trying to get the next row or if not present (end of list), return the first one.
ID (primary key) value
1 John
3 Bob
9 Mike
10 Tom
- If we look for 5, it should return the row with ID 9.
- If we look for 10, it should return the row with ID 1.
Currently, I simply run two queries, first getting the next:
SELECT * FROM table WHERE id > 5 order by id limit 1;
and then if this returns no result, return the first:
SELECT * FROM table WHERE id = 1;
But I was wondering if there was a way to do it in one go?
not existspart. But I guess your solution is more efficient – Mar 07 '21 at 08:39