mysql float data not selecting in where clause

后端 未结 5 1640
清酒与你
清酒与你 2020-12-16 20:18

This maybe an easy one but i couldn\'t get answer. I need to select float value from table

example table :-

value
10.2
4.5
4.6
4.06
<
相关标签:
5条回答
  • 2020-12-16 20:25

    you write:

     SELECT * FROM table where round(value, 1) = 4.6
    
    0 讨论(0)
  • 2020-12-16 20:28

    Use decimal instead of float.

    A decimal(10,2) will have 2 and only 2 decimal places and can be compared in the same manner as integers. Especially for monetairy values you should always use decimal, but anywhere where rounding errors are unwanted, decimal is a good choice.

    See: http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html

    Or MySQL DECIMAL Data Type Characteristics

    0 讨论(0)
  • 2020-12-16 20:44

    Today, I also came across the same situation and get resolved just by using FORMAT function of MySQL, It will return the results that exactly match your WHERE clause.

    SELECT * FROM yourtable WHERE FORMAT(`col`,2) = FORMAT(value,2)
    

    Explanation:

    FORMAT('col name',precision of floating point number)
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-16 20:51

    Generally, you should never check equality with floats (unless, potentially, you have the same object). Internally, it is represented with more precision, even if it isn't showing it to you by the time it outputs to the screen. This basic tenet holds true for computing in general.

    There are a dozens of schemes for doing this, but here is a simple one, which should make sense:

    SELECT * FROM table where value BETWEEN 4.599 AND 4.601
    
    0 讨论(0)
  • 2020-12-16 20:52

    You can also try query

    SELECT * FROM table WHERE value LIKE 4.6;
    
    0 讨论(0)
提交回复
热议问题