我有两个表,一个表有三列,另一个有两列。我需要从Table1中获取记录,而Table2中没有这些记录。例如
DECLARE @Table1 TABLE (C1 INT, C2 INT, C3 INT)
INSERT INTO @Table1 VALUES(1,1,1)
INSERT INTO @Table1 VALUES(1,2,2)
INSERT INTO @Table1 VALUES(1,3,3)
INSERT INTO @Table1 VALUES(2,1,4)
DECLARE @Table2 TABLE (C1 INT, C2 INT)
INSERT INTO @Table2 VALUES(1,1)
INSERT INTO @Table2 VALUES(1,2)
我需要如下所示的结果
C1 C2 C3
--------
1 3 3
2 1 4
答案 0 :(得分:2)
这应该有效:
SELECT Table1.*
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.C1 = Table2.C1
AND Table1.C2 = Table2.C2
WHERE Table2.C1 IS NULL
AND Table2.C2 IS NULL
答案 1 :(得分:1)
请试试这个 -
SELECT t1.*
from table1 t1
where not exists (select null from table2 t2
where t1.c1=t2.c1
and t1.c2=t2.c2);