PDA

View Full Version : nearest number


cancer10
03-08-2008, 01:45 PM
If you have a column, of data type = INT, with the following numbers
as separate rows.
1
3
4
5
7
10


How can you write a sql statement where you can calculate the
nearest number to any give input.
Example , if the input is 9 ? (the answer is 10)
If the input is 8 (the anwer is 7)

abduraooft
03-08-2008, 02:33 PM
select id from table_name where id>9 order by id ASC limit 1
Edit: this won't work since you need the nearest, not the next, sorry.

cancer10
03-08-2008, 02:37 PM
so for each row I have to write a separate query?

barkermn01
03-09-2008, 06:39 AM
try some thing like this
connect run a SELECT * FROM
i = 0;
then use a while($row = mysql_fetch_array($SQL))
{
i++

$Numbers[i] = $row['numbers']
}
Then See if you can use php the find the neret in the array not sure if it can do it tho

StupidRalph
03-10-2008, 04:09 AM
What if the number is 6? What would it return?