Q:

Nearest value , positive or not in mysql

I wonder how can I return a value in Mysql, being positive or not. Or even if there is a function that helps me to have a return.
Example: "select value from table where value = -10;"
My response would be the nearest of -10.
I am grateful.

A:

You can use the ABS() function to get the absolute value of the difference between a given value and a value from the table.  Then just order by that difference and take the top record for the "closest".  Something like this:
SELECT
  value,
  ABS(value - ?givenValue) AS difference
FROM
  table
ORDER BY
  difference
LIMIT
  1

The idea behind the difference being that A - B should be close to 0 for values which are close to the given value.  Using ABS() will just give you the distance from 0 for those values.  So ordering by that column should give you the records in distance from 0 on that comparison.

