大家好,我正在使用postgresql-13,我被困在这里并尝试了,但仍然没有得到我想要的结果:
TABLE_A
ID|SEQ|Name
1|7|Dash
2|8|Goo
TABLE_B
REF_ID|Country
1|India
3|England
TABLE_C
SEQ|Country
7|America
8|Denmark所以我想从TABLE_A做一个左连接来获取TABLE_B中的国家,但是如果在TABLE_B中找不到国家,我加入到TABLE_C中,这样Dash就会有印度国家,因为它可以在TABLE_B中找到,即使你加入SEQ到TABLE_C,它应该是美国,但我们可以在TABLE_B中找到"Dash“ID,所以我们从TABLE_B中获取国家。对于"Goo",他应该有一个国家丹麦,因为"Goo”ID在TABLE_B中找不到。
所以基本上左连接应该是
Select a.name,
case when b.country is null then c.country
when b.country is not null then b.country
end country_name
from TABLE_A a
left join TABLE_B b on a.ID=b.REF_ID
left join TABLE_C c on a.SEQ=c.Country这是正确的查询吗?
发布于 2021-06-04 17:03:26
table_a和table_c之间的密钥错误。您必须将c.country替换为c.seq.Your查询应为
Select a.name,
case when b.country is null then c.country else b.country end country_name
from TABLE_A a
left join TABLE_B b on a.ID=b.REF_ID
left join TABLE_C c on a.SEQ=c.seqhttps://stackoverflow.com/questions/67833933
复制相似问题