为什么Oracle使用不等于查不出来

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Rm_and_Rf/article/details/102514957

关于Oracle中的不等于号:
在Oracle中,

<>
!=
~=
^=

都是不等于号的意思。都可以使用。

在oracle中判断某个字段Sex不是“Y”时,

select * from t_table where sex <> 'Y';
但是sex是为空的,所以怎么查也查不出来。

那是因为 字段为null的时候,只能用 is null 和 is not null 来判断

需要将查询语句改成这样:
select * from t_table where sex <> 'Y' or sex  is null ;

注意:当使用or 和 and 拼接语句时,需要注意执行的先后顺序。where 后面如果有and,or的条件,则or自动会把左右的查询条件分开,即先执行and,再执行or。原因就是:and的执行优先级最高!
逻辑运算的顺序

SQL1: select count(*) from tableA where DEL_FLAG = '0' or DEL_FLAG is null and 1 <> 1 
相当于
SQL1: select count(*) from tableA where DEL_FLAG = '0' or (DEL_FLAG is null and 1 <> 1)
当判断第一个条件DEL_FLAG = '0' 满足时,就不再继续判断后面的条件了

而
SQL2: select count(*) from tableA where (DEL_FLAG = '0' or DEL_FLAG is null) and 1 <> 1
判断(DEL_FLAG = '0' or DEL_FLAG is null)这个条件的结果,并且要同时满足后面的 and 1<>1
显然 1<>1 是false

猜你喜欢

转载自blog.csdn.net/Rm_and_Rf/article/details/102514957