2017-04-05 96 views
1

當我比較where子句中的float值時,它不會給出正確的結果。 例如比較mysql中的double值似乎不能正常工作

SELECT * FROM users WHERE score = 0.61 

在此查詢分數是double類型

但上述查詢一欄,如果我檢查評分爲0.50沒有別的被搜索,而我有記錄與0.61

工作

上面的查詢還工作,如果我使用

SELECT * FROM users WHERE trim(score) = 0.61 
+0

分數列數據類型? – JYoThI

+0

是的,如果我使用'trim'函數,它就可以工作 –

+0

@VivekMaru http://stackoverflow.com/a/2567509/4248328 –

回答

1

我建議你使用decimal而不是float。它也只有2個小數位。

以下是關於如何使用它的文檔。 Link

我希望這能解決您的問題。

+0

如果我使用小數,它會自動附加0在剩餘的小數範圍內,例如'0.61'變成'0.6100',我不需要 –

+0

cast('score'AS CHAR)<=>'0.61'試試這個 – rahulsm

0

如果沒有沒有在您的浮動列中指定小數範圍內我不會沒有鑄造工作或修剪:

這工作得很好:

-- drop table test_float; 
create table test_float(f float(6,4) , d DECIMAL(4,2)); 
insert into test_float values (0.5,0.5); 
insert into test_float values (0.61,0.61); 
select * from test_float where f = d; 
select * from test_float where f = 0.61; 

這不起作用:

drop table test_float; 
create table test_float(f float , d DECIMAL(4,2)); 
insert into test_float values (0.5,0.5); 
insert into test_float values (0.61,0.61); 
select * from test_float; 
select * from test_float where f = d; 
select * from test_float where f = 0.61; 
select * from test_float where CAST(f as DECIMAL(16,2)) = 0.61; 

它十進制範圍range = 1 工作爲什麼,我真的不知道爲什麼?!