原因
not in
相當於all
,如果 not in
后面跟的是子查詢,子查詢中只要包含一個 null
的返回值,則會造成 整個 not in
字句返回空值,查詢不會返回任何結果。
但 in
相當於 any
,可以處理子查詢中返回null
的情況,返回正確的結果。
解決方案
為防止not in
返回空值,可以在子查詢的Where 語句后篩掉為空的記錄,例如:
select * from users u
where u.unionid not in
(SELECT su.s_unionid
from ninth_studio.staff_users su
where su.s_unionid is not null)