2017-05-08 99 views
-1

表1結合2個表

 ID  
    -------- 
    user1  
    -------- 
    user2  
    -------- 
    user3 
    -------- 
    user4  
    -------- 

表2

ID | Password 
------------------ 
user1 | xyz 
在Oracle SQL

,我怎麼能得到這樣的輸出?

輸出

ID | Password 
------------------ 
user1 | xyz 
------------------- 
user2 | NotAvailable 
------------------ 
user3 | NotAvailable 
------------------ 
user4 | NotAvailable 
------------------ 

回答

2

簡單。左連接。

select t1.id, 
    coalesce(t2.password, 'NotAvailable') as password 
from table1 t1 
left join table2 t2 on t1.id = t2.id; 
0

您可以使用外連接:

SELECT t1.id, NVL(t2.password,'NotAvailable') 
    FROM t1, t2 
WHERE t1.id = t2.id(+); 
0
select table1.id, 
    case when table2.password is null then 'NotAvailable' 
            else table2.password 
    end "Password" 
from table1 
left outer join table2 on table1.id=table2.id;