内部连接查询的相反

Nic*_*rca 36 t-sql sql-server inner-join outer-join

有人可以帮我写一个像这样的scernerio sql:

Table 1

2 columns: ID, Name

Table 2

2 columns: ID, Name
Run Code Online (Sandbox Code Playgroud)

我想要一个查询来显示表1中不在表2中的名称.因此,过滤掉表2中表2中的所有名称是结果查询.使用ID进行过滤而不是名称.

这将帮助我做我想做的事情.提前致谢

And*_*rew 58

Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
Run Code Online (Sandbox Code Playgroud)

  • NOT EXISTS要快得多.https://github.com/ebergstedt/sql_performance_left_outer_join_null_vs_not_exists (3认同)

Joe*_*lli 27

这应该比left join...is null版本更好.请参阅此处此处进行比较.

select t1.id, t1.name
    from table1 t1
    where not exists(select null from table2 t2 where t2.id = t1.id)
Run Code Online (Sandbox Code Playgroud)


DFo*_*k42 17

使用此查询

select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
Run Code Online (Sandbox Code Playgroud)

这可以通过将t1中的所有内容连接到t2中的任何内容来实现.where子句过滤掉t2中不存在的所有记录.