sql - SQL,判断表A中的记录是否存在于表B中
问题描述
TableA 有 AccountNum 和 RoutingNum 列
TableB 也有列 AccountNum 和 RoutingNum
表 B 更值得信赖。因此,我想查找表 A 中的所有记录是否都存在于表 B 中。如果不存在,哪些记录不匹配。
这是在正确的轨道上吗?您的解决方案将不胜感激。
select *
from TableA a
where a.AccountNum not in (select b.AccountNum from TableB)
解决方案
您可以使用not exists
. 以下查询为您提供了在tablea
中找不到的所有记录tableb
:
select a.*
from tablea a
where not exists (
select 1
from tableb b
where a.accountNum = b.accountNum and a.routingNum = b.routingNum
)
这假设您希望在两列上都匹配(这是您的描述所暗示的)。您可以调整where
子查询中的条件以仅匹配一列。
推荐阅读
- javascript - 我的 HTML 标记中有许多部分使用相同的类,如何为每个部分使用一个函数?
- javascript - 使用 crypto.subtle.digest 生成给定文件的摘要是否可行?
- typescript - babel-plugin-module-resolver 不适用于 react native 和 typescript
- java - 为什么可以将 java 可为空的返回值分配给 Kotlin Non-Nullable 变量?
- botframework - 微软机器人框架:turncontext
- c# - 由 Bot Framework 遥测生成的事件未出现在 Application Insights 中
- airflow - Airflow SSH 操作员错误:遇到 RSA 密钥,应为 OPENSSH 密钥
- prolog - 如何为其他谓词中的列表元素添加约束?
- javascript - 使用 SocketIO 和 node.js,只有我发送的消息在聊天室中不可见
- terraform - Terraform - 如何获取在 for_each 块中创建的资源的引用