有两个表TABLE1和TABLE2,其中有一个公共(public)字段ID。我想根据 ID 值从 TABLE2 中检索与 TABLE1 不匹配的值。
select * from TABLE2 where subject = 1 and ID NOT IN (select ID from TABLE1 where subject = 1)
示例:
TABLE1
ID SUBJECT
1 1TABLE2
ID SUBJECT
1 1
2 1
预期结果为 2,运行良好。
但是当TABLE1 为空 或inner select ID from TABLE1 where subject = 1
返回空时,整个select 语句返回空。
但预期结果是1、2
有什么办法可以实现吗?
最佳答案
使用左连接
select t2.*
from table2 t2
left outer join table1 t1 on t1.id = t2.id and t1.subject = 1
where t2.subject = 1
and t1.id is null
关于sql - 带有 NOT IN 的多个 SELECT - 内部 Select 语句返回空,整个 SELECT 返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16657611/