Reputation: 4279
Lets say we have a table like that
id|value
--------
1 | 50
2 | 19
3 | 100
4 | 21
5 | -10
How can I use ORDER BY
operator to order values by their distance to another value?
SELECT * FROM table ORDER BY nearest(value,30) DESC
To get this table:
id|value
--------
4 | 21
1 | 50
2 | 19
5 | -10
3 | 100
Upvotes: 2
Views: 510
Reputation: 17314
Not sure that all sql dialect accepts answer of Paul92.
Here is another solution:
SELECT *
FROM (
SELECT
t.*,
abs(value - 30) AS abs_value
FROM table t
) temp
ORDER BY abs_value
Upvotes: 2