开发者

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜