This question already has an answer here:
If I had a table something like
ID VALUE
1 1000
2 1500
3 2000
and I were to select value='1999'
it would return the 3rd row. If I were to select 1200
instead, it would return the 1st row. How do I do that in php?
</div>
What a nice question :)
SELECT VALUE FROM mytable ORDER BY ABS(VALUE-1999) LIMIT 1
Of course that 1999 will be a variable in real.
I didnt try it But i think it should work
$query="SELECT abs(value-'".$_REQUEST['myValue']."') as num,value from table order by num asc limit 1";
select id, value, ABS(1999 - value) as delta
from YOUR_TABLE
order by delta
LIMIT 1
That will calculate the delta between your request and all data in the table and will order by the smallest delta.. so if you limit the request to 1, you will have the nearest row !
Hope it will help you !
select id, value, ABS(1999 - value) as number
from tablename
order by number
limit 1