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

SQL: JOIN problem using temp tables and one column

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

我确实必须使用联接,但我的想法是,如果我在每个 table 的各个列上加入,然后在 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);