1

我有 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..

有任何想法吗?

4

2 回答 2

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
于 2012-07-26T01:06:23.203 回答
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
于 2012-07-26T02:59:15.290 回答