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)
posted @ 2019-04-22 18:28  清涛  阅读(1445)  评论(0编辑  收藏  举报