我有3张桌子: 表1
查询:
select table1.x
from table1 right outer join
table2
on table1.x = table2.x right outer join
table3
on table1.x = table3.x
但我只能看到table2和table3中的x值
答案 0 :(得分:1)
使用left join
,而不是right join
,以及适当的过滤条件:
select table1.x
from table1 left join
table2
on table1.x = table2.x left join
table3
on table1.x = table3.x
where table2.x is not null or table3.x is not null;
您可以考虑使用exists
:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.x = t1.x) or
exists (select 1 from table3 t3 where t3.x = t1.x);
这似乎是一种更自然的方式来实现您的逻辑 - 当x
或table2
中重复table3
时,您不会得到重复。