sql - 此应用程序中的自然联接还是外部联接?
问题描述
我有三个表,我需要将它们连接到一个输出表中,但似乎没有连接或逗号产生所需的输出
桌子是
E G | E S | E B
数据看起来像这样
e1 g1 | e1 s1 | e1 b1
e2 g2 | e2 s2 | e2 b2
select e1,g,s,b from
(select e as e1, m as g from table x where m=g),
(select e as e2, m as s from table x where m=s),
(select e as e3, m as b from table x where m=b)
where e1=e2 AND e2=e3;
我想要的输出是
E | G | S | B
e1| g1| s1| b1
e2| g2| s2| b2
但我得到了乘法结果
e1 g1 s1 b1
e1 g1 s1 b2
e1 g1 s2 b1
e1 g1 s2 b2
我尝试使用自然连接而不是逗号,但我没有得到想要的输出,我不明白自然连接应该很容易解决问题的问题,对吧?
解决方案
使用相关查询:
select e.col1 E,e.col2 G,(select s.col2 from s where s.col1=e.col1 ) S,
(select
b.col2 from b where b.col1=e.col1 ) B
from e;