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