Mysql 获取不包含';t存在于一个表中,但存在于另一个表中

Mysql 获取不包含';t存在于一个表中,但存在于另一个表中,mysql,Mysql,我一直试图通过以下查询获取表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 Ja

我一直试图通过以下查询获取表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

尝试一系列左连接,然后检查NULL

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

实际上非常简单…只需
连接
表,并为您要排除的表设置
null

select * FROM TableA a
left join TableB b 
  on a.ref1 = b.ref1 
    where b.ref1 is null
复制