Page 1 of 1

[SOLVED] closest number?

Posted: Mon Jun 14, 2004 11:38 am
by shark86
Hey,
I have a problem.

I have a DB table with a collume in witch i saved numbers.
1.123
1.234
4.67854
...
I need to get the number that is closest or equal to 2.3. In that example 1.234.
How do I do that? If I use like or = in a MySQL DB query I get just a replay if 2.3 is in the DB, but not the closest.
I looked a usort, but i wasen't able to do it.

greetings an thanks
shark86

Posted: Mon Jun 14, 2004 11:59 am
by Weirdan

Code: Select all

select numberr from numbers order by abs($num_to_search_for-numberr) limit 1

Posted: Tue Jun 15, 2004 11:39 am
by shark86
Thank you so much, it works fine

shark
http://www.sebastian-haak.net