我有4个表,其中4个是一个联结表中的外键。
问题是我需要检索缺少的jnction值。
例如:
Table A Table B Table C Table D
-------- --------- --------- ----------
1 4 7 A
2 5 8 B
JUNCTION TABLE
-----------------
1 4 7 A
1 4 7 B
1 4 8 A
1 4 8 B
1 5 7 A
1 5 7 B
1 5 8 A
1 5 8 B
2 4 7 A
2 4 7 B
2 4 8 A
2 4 8 B
2 5 7 A
2 5 7 B
2 5 8 A
2 5 8 B
因此,如果缺少列1 5 8 B,当我运行查询时,它显示1 5 8 B ..
任何想法?
答案 0 :(得分:1)
我会在源表上使用交叉连接,然后将其结果加入到联结表中。
编辑:感谢andy holaday指出MySQL没有FULL OUTER JOIN。
select x.*
from (select *
from a,b,c,d) as x
left outer join j
on (x.a = j.a and
x.b = j.b and
x.c = j.c and
x.d = j.d)
where j.a is NULL and
j.b is NULL and
j.c is NULL and
j.d is NULL
答案 1 :(得分:0)
使用此查询,该查询从4个表交叉连接到外部连接:
select a.col, b.col, b.col, d.col
from tablea a
cross join tableb b
cross join tablec c
cross join tabled d
left join junction j
on j.cola = a.col
and j.colb = b.col
and j.colc = c.col
and j.cold = d.col
where junction.cola is null; -- sufficient to test just one