user1814358
user1814358

Reputation: 619

Select equal or nearest greater number from table?

I know how i get nearest value, using abs() function in order by like this:

select BetScope from `table` 
order by abs(BetScope - 0.50) limit 1;

So let say that 0.50 does not exists, but there are 0.25 and 0.75, both are same range from 0.50.

Here advatnage has smaller number (here is 0.25), how i can sort table that first is 0.75 if both numbers are in same range from search number?

Upvotes: 2

Views: 32

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 176284

You can ORDER BY 2 columns:

select BetScope 
from `table` 
order by abs(BetScope - 0.50), BetScope DESC   -- prefer higher BetScope when tie
limit 1;

Upvotes: 3

Related Questions