Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql float data not selecting in where clause

Tags:

types

mysql

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

my query

SELECT * FROM table where value = '4.6'

returns empty result set

how can i solve this !

like image 616
Gowri Avatar asked May 26 '11 07:05

Gowri


People also ask

How do you represent a FLOAT in MySQL?

MySQL permits a nonstandard syntax: FLOAT( M , D ) or REAL( M , D ) or DOUBLE PRECISION( M , D ) . Here, ( M , D ) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) is displayed as -999.9999 .

How do I save a FLOAT in MySQL?

MySQL uses four bytes to store a FLOAT value. DOUBLE is a double precision floating point number. MySQL uses eight bytes to store a DOUBLE value. MySQL treats DOUBLE as a synonym for DOUBLE PRECISION (a non-standard extension).


2 Answers

You can also try query

SELECT * FROM table WHERE value LIKE 4.6;
like image 91
Zhongjun 'Mark' Jin Avatar answered Sep 23 '22 00:09

Zhongjun 'Mark' Jin


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
like image 31
John Green Avatar answered Sep 26 '22 00:09

John Green