91名师指路-头部
91名师指路

mysql使用float类型进行精确查找竟然查询不到数据?

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

闲谈:之前一直没注意到这个问题,今天在修复一个生产bug时遇到这个问题。


一:创建一张测试表


二:往里面插入测试数据


三:测试(使用score字段)

3.1)根据一下条件均无法查询到结果

select * from t_test where score = 40.1
select * from t_test where score = 40.2
select * from t_test where score = 40.3
select * from t_test where score = 40.4
select * from t_test where score = 40.6
select * from t_test where score = 40.7
select * from t_test where score = 40.8
select * from t_test where score = 40.9


3.2)根据一下条件可以查询到结果

select * from t_test where score = 40
select * from t_test where score = 40.5


3.3)使用范围进行查询

select * from t_test where score >= 40.1 and score <= 40.4


查询结果(不包括40.1和40.4):



总结:

到此我们似乎发现了什么规律:在mysql中如果数据类型是float时,如果值为正数(如40)或者小数位是5时
(如40.5)可以进行精确查询,如果不是整数并且小数位不是5是无法进行精确查询只能进行范围查询。


四:解决方法

方法一:将float类型转为double或decimal类型。


方法二:将float转成字符串在进行精确查询。

select * from t_test where concat(score, '') = 40.1


查询结果:





2023-08-17 15:35:22     阅读(306)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部