首页 > 解决方案 > 两个表之间的 SQL 连接帮助

问题描述

我有两个表表 a 和表 b。

表 b 包含表 a 的子集。我想获取不在表 b 中的表 a 的所有 id ID(id 是一个公共列) 尝试使用此 SQL,但它不起作用 select name from table a where name not in

select unique name
from table a inner join
     table b
     on a.id = b.id;  

感谢你的帮助

标签: sqloraclejoin

解决方案


一种方法是通过使用left outer join.

在 oracle 中,您还可以使用exists

with TableA as (
  select 1 as "COL1" from dual
  union all select 2 from dual
  union all select 3 from dual
  union all select 4 from dual
  union all select 5 from dual
), TableB as (
  select 1 as "COL1" from dual
  union all select 2 from dual
  union all select 3 from dual
)
select * 
from TableA
where not exists(select * 
                 from TableB 
                 where TableA.COL1 = TableB.COL1)

推荐阅读