how to get nearest value from database in mysql

后端 未结 11 1092
夕颜
夕颜 2020-12-31 16:39

I am using mySQL and CodeIgniter. I have some floating point numbers in my database such as

  • 8.3456
  • 8.5555
  • 4.5556
相关标签:
11条回答
  • 2020-12-31 17:26
    SELECT * FROM table1 ORDER BY ABS(value - '$myvalue') LIMIT 1 
    
    0 讨论(0)
  • 2020-12-31 17:26

    Read this page http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html#function_round

    but your select would look like this

    select value from table where ROUND(value) = $myvalue 
    
    0 讨论(0)
  • 2020-12-31 17:29
    select * 
    from table 
    order by abs(value - $myvalue)
    limit 1
    
    0 讨论(0)
  • 2020-12-31 17:31
    SELECT number, ABS( number - 2500 ) AS distance
    FROM numbers
    ORDER BY distance
    LIMIT 6
    

    Selecting closest values in MySQL

    0 讨论(0)
  • 2020-12-31 17:31

    Try this:

    SELECT *,abs((columnname -Yourvalue)) as near
      FROM table
     WHERE order by near limit 0,1
    
    0 讨论(0)
提交回复
热议问题