2014-09-10 112 views
0

我對一個視圖運行一個非常簡單的查詢,它使用一個值,並且不能與其他值一起工作。我試圖從不同的索引值視圖中選擇行只指數1.5返回的結果關於Mysql查詢查詢無法正常工作

以下是查看錶的樣本

我有這種說法easylens

select * from easylens 

+---+----+------+----+-----+-----+-----+-------+--------+ 
|id |type|design|name|brand|index|color|coating|material| 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 1 | sv |aase |nel |hoya | 1.5|292 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 2 | sv |base |tel |zeri | 1.5|293 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 3 | sv |case |fel |essi | 1.5|294 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 4 | sv |dase |gel |hoya | 1.6|293 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 5 | sv |fase |rel |essi | 1.6|293 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 6 | sv |gase |mel |hoya | 1.6|292 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 

當我運行

select * from easylens where `index`=1.5 

我得到

+---+----+------+----+-----+-----+-----+-------+--------+ 
|id |type|design|name|brand|index|color|coating|material| 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 1 | sv |aase |nel |hoya | 1.5|292 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 2 | sv |base |tel |zeri | 1.5|293 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 
| 3 | sv |case |fel |essi | 1.5|294 |ar  |plastic | 
+---+----+------+----+-----+-----+-----+-------+--------+ 

但是當我運行

select * from easylens where `index`=1.6 

我得到

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0002 sec) 
+1

w^hat是'index'字段的數據類型? – Sadikhasan 2014-09-10 10:51:05

+0

@Sadikhasan索引字段爲float – Bakly 2014-09-10 10:57:31

+1

將其更改爲DECIMAL - 或者它可能足以在FLOAT中定義精度和比例 - 但我不確定爲什麼 – Strawberry 2014-09-10 11:00:20

回答

1

浮動的危險(與不確定的精度和小數)

SELECT * FROM easylens WHERE `index` = 1.6000000238418578; 
+----+------+--------+------+-------+-------+-------+---------+----------+ 
| id | type | design | name | brand | index | color | coating | material | 
+----+------+--------+------+-------+-------+-------+---------+----------+ 
| 4 | sv | dase | gel | hoya | 1.6 | 293 | ar  | plastic | 
| 5 | sv | fase | rel | essi | 1.6 | 293 | ar  | plastic | 
| 6 | sv | gase | mel | hoya | 1.6 | 292 | ar  | plastic | 
+----+------+--------+------+-------+-------+-------+---------+----------+ 
1

嘗試投

SELECT * FROM easylens WHERE CAST(`index` as DECIMAL) = CAST(1.6 as DECIMAL); 
+0

@Bakly你檢查這個查詢它適合你嗎? – Sadikhasan 2014-09-10 11:17:27

+0

它不是關於指定長度,而是關於指定長度十進制(M,D)浮點數(M,D)也不起作用 – Bakly 2014-09-10 11:45:26