2015-06-12 30 views
0

我有組合2個表的問題。可以將2個表與這些值結合起來?SQL查詢:將兩個表與空值組合

表1:

id no. descrp  value 
1   A  10 
3   C  30 
5   E  50 

表2:

id no. descrp 
1   A 
2   B 
3   C 
4   D 
5   E 

結果:

id no. descrp value 
1   A   10 
2   B   null/0 
3   C   30 
4   D   null/0 
5   E   50 

我已經嘗試加入2個表,但結果無法顯示空值。

+1

搜索'LEFT JOIN'。 –

回答

0

您只需要一個Left Outer Join,它選擇位於左側的表中的所有數據,並在右側的表格位置中找到等同的匹配數據。如果匹配,則它返回數據,否則它填充NULL。在這裏,你去..

SELECT t2.[id no], t2.Descrption, t1.amount 
FROM table2 t2 
Left outer join table1 t1 on t2.[id no]= t1.[id no] 

編輯 - 與,使用下面的查詢,以取代NULL

SELECT t2.[id no], t2.Descrption, COALESCE(t1.amount,0) FROM table2 t2 Left outer join table1 t1 on t2.[id no]= t1.[id no] 
+0

我得到了相同的結果sir ...不能顯示空值... – aoy24

+1

@ aoy24試試這個.... SELECT t2。[id no],t2.Descrption,COALESCE(t1.amount,0) FROM table2 t2 左外部連接table1 t1上t2。[編號] = t1。[編號] –

+0

是啊...得到它先生@ Amnesh Goel ...非常感謝.... – aoy24