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

时间:2017-12-22 11:24:27

标签: sql oracle join

我有两张桌子

**Location**
id int 
name varchar

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

出生地 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

未提供所需结果。非常感谢任何帮助

1 个答案:

答案 0 :(得分: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