Mysql 用 not in 不返回任何結果的原因及解決方案


原因

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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM