Sql 获取其他表中不存在的记录
我有一个超过1000行的临时表,但举例来说如下 #桌子 然后我有另一个表,如下所示(这不是临时表) 合规表Sql 获取其他表中不存在的记录,sql,sql-server,Sql,Sql Server,我有一个超过1000行的临时表,但举例来说如下 #桌子 然后我有另一个表,如下所示(这不是临时表) 合规表 ID | Code 33623 | 421230 33627 | 421230 33637 | 421230 33641 | 200585489 33642 | 200585489 33623 | 45109 如何根据临时表的ID和代码检查临时表中的记录是否不存在于符合性表中 因此,根据以上数据,我希望得到以下结果 不匹配的项目: 输出表 Id | Code 33641 |
ID | Code
33623 | 421230
33627 | 421230
33637 | 421230
33641 | 200585489
33642 | 200585489
33623 | 45109
如何根据临时表的ID和代码检查临时表中的记录是否不存在于符合性表中
因此,根据以上数据,我希望得到以下结果
不匹配的项目:
输出表
Id | Code
33641 | 421230 //doesnt exists
33642 | 421230 //doesnt exists
33623 | 200585489 //doesnt exists
从#temp表3中的4条记录中,上述记录在合规性表中不存在只需使用
不存在
:
select t.*
from #table t
where not exists (select 1
from compliance c
where c.id = t.id and c.code = t.code
);
可以使用左外连接,如下所示:
select t1.* left outer join t2 on t1.id = t2.id where t2.id is null
select t1.* left outer join t2 on t1.id = t2.id where t2.id is null