sql中not in 和 not exists

使用not in


select a.* from  HRPersonnel as a 
where a.unitid=25 
and PersonnelId  not in 
(select PersonnelId from HRSalaryDeploy  )

始终查询不出来数据,但是括号中的数据集PersonnelId 和左侧的的确有差异。
查了查发现:当not in 的子集中存在null值的时候,返回的结果肯定是null,所以我查不出数据
这个时候就要使用not exists


select a.* from  HRPersonnel as a 
where a.unitid=25 
and  not exists 
(select PersonnelId from HRSalaryDeploy as b where a.PersonnelId=b.PersonnelId )


版权声明:本文为zhongaiyisheng_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。