Get the ID of a next greater/less Value imagine you have follwing table given




id
value




1
5


2
3


3
7


4
1




Depends on your case you need the ID of the next smaller
or greater value.
So the select of ID=2 would give u either 4 or 1.
How would you solve that?
thanks in advice.
Eing
You can just join the table to itself on the id field = the value + 1 and again on value - 1. My example uses an INNER JOIN which will only return the cases where both match, otherwise you'll want to use the appropriate OUTER JOINS or two separate queries, to not filter out.
SELECT * -- Replace with only the columns you need as SELECT * is bad practice
FROM SomeTable ST1
INNER JOIN SomeTable ST2
    ON ST2.id = ST1.value + 1
INNER JOIN SomeTable ST3
    ON ST2.id = ST1.value - 1