webmasters
webmasters

Reputation: 5831

Sql value is closest or equal to a certain value in a field

I have a mysql table with a field called POSTIDS POSTIDS values: 1,2,6,7,15,24,25,63,78,98,120

I have a variable called $postid = '21'

SELECT * FROM table WHERE POSTIDS "is closest" to $postid (in this case 21)

How can i do this?

Upvotes: 0

Views: 336

Answers (1)

Randy
Randy

Reputation: 16677

select * 
FROM table
where abs($postid-POSTIDS) = (select min( abs($postid-POSTIDS))
      from table )

Upvotes: 1

Related Questions