2014-02-26 55 views
0

我一直试图通过以下查询获取表A中存在但不包含在表B中的记录(如结果中所示)一个空表获取表中不存在但存在于另一个表中的记录

查询:

select * FROM TableA 
where ref1 not in (select ref1 from TableB) 
and ref2 not in (select ref2 from TableB) 
and ref3 not in (select ref3 from TableB) 

表A:

ref1 ref2 ref3 qte 
VT1  Jaune L  100 
VT1  Jaune XL  100 
VT1  GRIS L  100 
VT1  GRIS XL  100 
VT2  Jaune L  100 
VT2  Jaune XL  100 
VT2  GRIS L  100 
VT2  GRIS XL  100 

表B:

ref1 ref2 ref3 qte 
VT1  Jaune L  100 
VT1  GRIS L  100 
VT2  Jaune L  100 
VT2  GRIS L  100 
VT2  GRIS XL  100 

结果:

ref1 ref2 ref3 qte 
VT1  Jaune XL  100 
VT2  Jaune XL  100 
+0

重复? http://stackoverflow.com/questions/367863/sql-find-records-from-one-table-which-dont-exist-in-another –

回答

0

尝试了一系列的LEFT JOIN的,然后检查空

select * 
FROM TableA 
LEFT OUTER JOIN TableB b1 
ON TableA.ref1 = b1.ref1 
LEFT OUTER JOIN TableB b2 
ON TableA.ref2 = b2.ref2 
LEFT OUTER JOIN TableB 3 
ON TableA.ref3 = b3.ref3 
WHERE b1.ref1 IS NULL 
AND b2.ref2 IS NULL 
AND b3.ref3 IS NULL 

虽然看到你想要,而不是我想这可能做到这一点的解释结果: -

select * 
FROM TableA 
LEFT OUTER JOIN TableB b1 
ON TableA.ref1 = b1.ref1 
AND TableA.ref2 = b1.ref2 
AND TableA.ref3 = b1.ref3 
WHERE b1.ref1 IS NULL 
0

其实很简单...只是join表,并设置为null TABL e你想排除的内容:

相关问题