在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