如何从Oracle中的两列连接两个表

问题描述 投票:0回答:1

我有两张桌子

**Location**
id int 
name varchar

**User**
id int
name varchar
birthplace int
living_in int

其中birthplace和living_in是对位置id的引用

我想将输出显示为

    Name Birthplace Living in
    Joe  LA         NY
    Bill Sac        Orl

我的查询

    select a.name, h.name as Birthplace, h.name as Living In
    from User a
    left join location h
    on a.birthplace= h.id
    left join location h
    on a.living_in = h.id

没有给出所需的结果。任何帮助是极大的赞赏

sql oracle join
1个回答
3
投票

使用适当的表别名,它变得更容易:

select u.name, b.name as Birthplace, l.name as LivingIn
from User u
left join location b
    on u.birthplace= b.id
left join location l
    on u.living_in = l.id
© www.soinside.com 2019 - 2024. All rights reserved.