繁体   English   中英

SQL:使用临时表和一列的联接问题

[英]SQL: JOIN problem using temp tables and one column

我创建了两个临时表,其中 TABLE1 包含所有项目,而 TABLE2 仅包含 TABLE1 的部分列表。 我怎样才能找出表 1 有哪些部分 TABLE2 没有,反之亦然? 请记住,由于 DISTINCT 语句,临时表只有一列。

我确实必须使用连接,但我的想法是,如果我在每个表的各个列上加入,然后在 Where 子句中声明第 1 列不等于第 2 列,这是矛盾的。


IF EXISTS   (
            SELECT *
            FROM tempdb.dbo.sysobjects
            WHERE id = Object_id(N'tempdb..#TABLE1')
            )
        BEGIN
            DROP TABLE #TABLE1
        END

IF EXISTS   (
            SELECT *
            FROM tempdb.dbo.sysobjects
            WHERE id = Object_id(N'tempdb..#TABLE2')
            )
        BEGIN
            DROP TABLE #TABLE2
        END
------------------------------------------------
select distinct 1.parts as #TABLE1 from List1 1  --- MAIN LIST

select distinct 2.parts as #TABLE2 from List2 2  --- ADDITIONAL LIST

select *
from #TABLE2 left join
     #TABLE1
     on 2.parts = 1.parts
where 2.parts <> 1.parts 

您的where子句正在撤消left join 我建议not exists

select t1.*
from #table1 t1
where not exists (select 1 from #table2 t2 where t2.parts = t1.parts);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM