ORACLE JOIN TABLES

时间:2016-04-28 12:43:55

标签: sql oracle

我有

T1: USER_ID OSX
       1     Y
       2     Y  

T2: USER_ID ANDROID
       1     Y
       3     Y  

我想按照以下方式加入表格,但我不知道如何

T3: USER_ID ANDROID  OSX
       1     Y        Y
       2     null     Y
       3     Y        null

2 个答案:

答案 0 :(得分:2)

您需要full outer join

select coalesce(t1.user_id, t2.user_id) as user_id,
       t2.android, t1.osx
from t1 full join
     t2
     on t1.user_id = t2.user_id;

答案 1 :(得分:0)

使用full join,更好地处理空值:

select coalesce(t1.user_id, t2.user_id) as user_id,
       coalesce(t2.android,'N/A') as android,
       coalesce(t1.osx,'N/A') as osx
from t1
full join t2 on t1.user_id = t2.user_id