在oracle中判断字段id不是“123”时,
select * from user where id<> '123';
但是id为空的,却怎么也查询不出来。
这是why?原因是:字段为null的时候,只能通过is null或者is not null来判断。
这样写才是正确的:
select * from user where id <> '123' or id 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