mysql的float条件查询不到结果,sql的一些其他问题

记一下今天遇到的问题

1. float用=匹配不到结果

sql:
  select * from table1 where low=74.9  # 这个sql查不到结果,但是实际上数据库里面有,low是float类型
  将low字段换成decimal类型,就可以查出来结果
原因:
  float类型存在误差,不能使用=号来比较值。大部分用float类型的字段,应该都是取范围做比较的。
  如果没有特别要求,设计数据库的时候,建议使用decimal类型

 2. 其他sql(有知道原因的老哥,方便的话,可以在评论区指点一下,mysql版本:5.7.36)

先创建一个表
create TEMPORARY table temp_0716 ( id serial, fl decimal(
16,3), bo boolean ); insert into temp_0716(fl,bo) values (22.3,false) (22.3,true);

sql1:
select * from temp_0716 where id='1 or 1=1'

能查到id=1的数据
sql2:
select * from temp_0716 where bo='true'
select * from temp_0716 where bo='false'

这两条语句执行的结果,查到的是同一条数据。但是把引号去掉就不是同一条了

 

 

posted @ 2022-07-16 11:41  10132714  阅读(529)  评论(0编辑  收藏  举报