将一个单独的列从一个表合并到另一个新表下

omjgkv6w  于 2021-04-02  发布在  Hive
关注(0)|答案(2)|浏览(377)

我正在使用两张表,两张表都有一个同名的id列,我需要找到一种方法将一张表的id合并到另一张表中,用一个新的列名。

Table 1

Houses
--------------------------------------
| ID |  Address                    
--------------------------------------
| 1  |  123 Main
| 2  |  234 Center
| 3  |  345 North Street

Table 2
Houses that are blue
--------------------------------------
| ID |  Address
--------------------------------------
| 2  |  234 Center

Resultant table:  
Houses

--------------------------------------
| ID |  Address          |  BlueHouseID
--------------------------------------
| 1  |  123 Main         |  NULL
| 2  |  234 Center       |  2
| 3  |  345 North        |  NULL

谢谢你在设置这个查询上的任何帮助。这个最终将被放在一个覆盖的文本文件中,以便以后的摄取。

bvpmtnay

bvpmtnay1#

使用 "left join"。

select h.*, hb.id as blue_id
from houses h left join
     houses_blue hb
     on h.id = hb.id
nafvub8i

nafvub8i2#

只是 "向左加入"。

select h.*, b.id as blue_house_id
from houses h
left join blue_houses b on b.id = h.id

或者你想在地址上匹配,而不是在id上匹配。

select h.*, b.id as blue_house_id
from houses h
left join blue_houses b on b.address = h.address

相关问题