在不等于的子查询中使用 Not in

using Not in in SubQuery with not equal to

通过以下方式交换条件,以下两个查询会产生不同数量的记录:

select count(1) from clientlist where userid  
 not in (select distinct userid from Clientlist
    where userid in (select uniqueid from employee e where emplstatus = 'Y' ))

返回38885条记录

select count(1) from clientlist where userid  
 in (select distinct userid from Clientlist
    where userid in (select uniqueid from employee e where emplstatus != 'Y' ))

返回3630条记录

谁能解释一下条件的变化导致结果有如此大差异的原因是什么?

  Table ClientList      Table employee
  Id                    Id   Status
  --                    --   ------
  1                     1    Y 
  2                     2    N
  3                     3    null
  4

第一个查询计数 id:234 - 在 ClientList 中但不在 employee 中且标记为 Y 的行.

第二个查询:id 2 - 显示在 tables 和 table 2 中的行被标记为 'N'.