I have a MySQL table in which one of the fields is variation and type is float. Its value is 0.2. But when I query like WHERE variation = '0.2' it is not showing the result, instead it returns null. What I am doing wrong?
SELECT * FROM tbl_products
WHERE frequency BETWEEN '2' AND '3'
AND gain = '2'
AND gain_variation = '0.2'
AND noise = '2'
AND power = '10'
Remove the quotes around the number a try again.
eg
WHERE variation = 0.2Edit: As Peeka pointed out, the quotes don’t matter in MySQL.
Are you absolutely sure the value of the float is exactly 0.2 and there aren’t significant digits that are getting rounded? Can you try using
BETWEEN 0.19 AND 0.21and seeing whether the row you’re after gets returned (I know it’s not an equivalent query and may return unwanted results but it will test whether the value is exactly 0.2 or whether it’s rounded in the client).Edit 2: I’d wager it’s because of the way MySQL stores floats – taken from their manual:
See here for more info: http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
It is recommended to use a range to filter instead of an equality, so if you use
BETWEEN 0.19 AND 0.21do you get the result you want?