Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 获取其他表中不存在的记录_Sql_Sql Server - Fatal编程技术网

Sql 获取其他表中不存在的记录

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 |

我有一个超过1000行的临时表,但举例来说如下

#桌子

然后我有另一个表,如下所示(这不是临时表) 合规表

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