mysql中慎用or ,or表示并集,or左边表达式的条件不会限制or右边的表达式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20009015/article/details/86663258

mysql中慎用or ,or表示并集,or左边表达式的条件不会限制or右边的表达式。

项目中了踩了一下这个坑
在这里记下来

select * from t_invoice_detail where
Date(make_invoice_time) >= ‘2018-11-09’ and Date(make_invoice_time) <=‘2018-10-31’
and tax_rate
in (0.17,0.16)

select * from t_invoice_detail where
Date(make_invoice_time) >= ‘2018-11-09’ and Date(make_invoice_time) <=‘2018-10-31’
and tax_rate =0.17
or tax_rate=0.16

这两个查询结果是不一样的。
第一个是限定了在tax_rate在0.17和0.16中 且make_invoice_time在闭区间2018-11-09到2018-10-31中的所有结果

第二个则是tax_rate在0.17中且make_invoice_time在闭区间2018-11-09到2018-10-31中的所有结果
再加上tax_rate在0.16中的所有结果

实例
select count() from t_invoice_detail where
Date(make_invoice_time) >= ‘2018-11-09’ and Date(make_invoice_time) <=‘2018-10-31’
and tax_rate
in (0.17,0.16)
union all
select count(
) from t_invoice_detail where
Date(make_invoice_time) >= ‘2018-11-09’ and Date(make_invoice_time) <=‘2018-10-31’
and tax_rate =0.17
or tax_rate=0.16

结果展示如下
在这里插入图片描述

第一个根本就没有符合条件的 ,而第二个则有一条

猜你喜欢

转载自blog.csdn.net/qq_20009015/article/details/86663258
今日推荐