开发者

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

开发者 https://www.devze.com 2023-01-26 18:13 出处:网络
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\'

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

精彩评论

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