2015-12-28 27 views
-1

我想使用2個SELECT語句和INNER JOIN從不同的查詢但也想顯示兩個不同的結果,從同一查詢中的不同表。像這樣..內部連接兩個SELECT語句在一個查詢AND顯示結果從兩個表

SELECT column1 FROM earth e1 that is null 
        + 
    SELECT chair5 FROM space s1 that is not null 

    INNER JOIN space s1 ON e1.car = s1.truck 

    ORDER BY e1.column,s1.chair5 

如何在使用INNER JOIN時顯示兩個不同查詢的結果?

+0

你是說你想要打印的select語句的結果以及隨後的內部連接的結果打印? –

+0

是的,我需要他們都打印和兩個表內部由類似列連接 – TrickyDBA

+0

請提供兩個表的示例數據,以及預期結果的示例。否則,它不是很清楚。 – sstan

回答

0

假設表T1包含的值'A','B','C'和表T2包含的值'A','B','D'

您可以查詢

select 'T1' as source, col from t1 
union all 
select 'T2' as source, col from t2 
union all 
select 'join T1,T2' as source, t1.col from t1 inner join t2 
on t1.col= t2.col 
order by 1,2 
; 

得到

SOURCE  COL 
---------- ----- 
T1   A  
T1   B  
T1   C  
T2   A  
T2   B  
T2   D 
join T1,T2 A 
join T1,T2 B 

第一列標識源:單個查詢或加入

或者,我會更喜歡你可能會使用FULL OUTER相同的信息(更壓縮)JOIN

with fj as (
select t1.col t1_col, t2.col t2_col 
from t1 full outer join t2 
on t1.col= t2.col 
) 
select 
case when t1_col is not null and t2_col is not null then 'both' 
when t1_col is not null then 'T1 only' 
when t2_col is not null then 'T2 only' end as source, 
nvl(t1_col, t2_col) col 
from fj 
order by 1,2 

SOURCE COL 
------- ---------- 
T1 only C   
T2 only D  
both A  
both B 
+0

但在OP的例子中,我們不知道column1和chair5是相同的屬性類型,那麼如何聯合這些元組呢? –

+0

我嘗試提供更好的數據示例。對不起 – TrickyDBA

+0

@TrickyDBA簡單地更新您的問題,如果答案不匹配 –