2014-09-10 115 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