WHERE
table_three.part_code IN(
^^
编辑
以下是一些满足要求的替代方案:Gief 表 3 中的所有行,以便零件代码存在于表 1 或表 2 中。
select t3.part_code
,t3.name
from table_three t3
where part_code in(select t1.part_code from table_one t1)
or part_code in(select t2.part_code from table_two t2);
带有联合的派生表
select t3.part_code
,t3.name
from table_three t3
join (select part_code from table_one
union
select part_code from table_two
) t12
on(t3.part_code = t12.part_code);
与联合的内连接
select t3.part_code
,t3.name
from table_three t3
join table_one t1 on(t3.part_code = t1.part_code)
union
select t3.part_code
,t3.name
from table_three t3
join table_two t2 on(t3.part_code = t2.part_code);
奖金。我不知道我为什么这样做。
select t3.part_code
,t3.name
from table_three t3
left join (select distinct part_code
from table_one) t1 on(t3.part_code = t1.part_code)
left join (select distinct part_code
from table_two) t2 on(t3.part_code = t2.part_code)
where t3.part_code = t1.part_code
or t3.part_code = t2.part_code;
让我知道他们是如何工作的。
编辑 2。
好的,试试下面的。它应该产生表 T1 和 T2 的并集。然后对于每一行,如果可以找到这样的零件代码,它将从 T3 中选择名称。
如果 part_code 是所有表中的键,您可以改为使用UNION ALL。
select T12.part_code
,coalesce(T3.name, T12.name) as name
from (select part_code, name from table_one T1 union
select part_code, name from table_two T2
) T12
left join table_three T3 on(T1.part_code = T3.part_code);