patternsqlMinor
Select next or first row
Viewed 0 times
selectrowfirstnext
Problem
For a given ID, I'm trying to get the next row or if not present (end of list), return the first one.
Currently, I simply run two queries, first getting the next:
and then if this returns no result, return the first:
But I was wondering if there was a way to do it in one go?
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?
Solution
You can combine the two queries:
Another option is to use an OR condition:
The
Typically OR conditions aren't optimized well, so the first alternative might be faster.
with t as (
select *
from the_table
where id > 5
order by id limit 1
)
select *
from t
union all
select *
from the_table
where id = 1
and not exists (select *
from t);Another option is to use an OR condition:
select *
from the_table
where (id > 5 or id = 1)
order by id = 1, id
limit 1The
order by id = 1 will sort the row with id = 1 at the end and all others (where that condition is false) will be sorted by the actual id value. The disadvantage is that you need to repeat the search conditionTypically OR conditions aren't optimized well, so the first alternative might be faster.
Code Snippets
with t as (
select *
from the_table
where id > 5
order by id limit 1
)
select *
from t
union all
select *
from the_table
where id = 1
and not exists (select *
from t);select *
from the_table
where (id > 5 or id = 1)
order by id = 1, id
limit 1Context
StackExchange Database Administrators Q#286568, answer score: 3
Revisions (0)
No revisions yet.