2012-10-29 67 views
0

我需要使用連接2表顯示所有數據。如何使用全外連接?

考試

表聯繫

numCode | fullName 
00001 | Midna 
00002 | Klog 
00003 | Porla 
00004 | Seka 
00005 | Mila 

表dateFile

numCode | dateCurr 
00001 | 2012-10-29 00:00:00.000 
00002 | 2012-10-29 00:00:00.000 
00005 | 2012-10-29 00:00:00.000 

用SQL服務器

SELECT df.numCode as 'numCode', tf.dateCurr as 'dateCurr' 
FROM dateFile df Full Outer join Contact ct On ct.numCode = df.numCode 
WHERE df.dateCurr = '2012-10-29' 

輸出

numCode | dateCurr 
00001 | 2012-10-29 00:00:00.000 
00002 | 2012-10-29 00:00:00.000 
00005 | 2012-10-29 00:00:00.000 

但我需要輸出得到:

numCode | dateCurr 
00001 | 2012-10-29 00:00:00.000 
00002 | 2012-10-29 00:00:00.000 
00003 | 2012-10-29 00:00:00.000 (Insert Date from choose datetime) 
00004 | 2012-10-29 00:00:00.000 (Insert Date from choose datetime) 
00005 | 2012-10-29 00:00:00.000 

回答

1

嘗試:

SELECT df.numCode as 'numCode', 
     coalesce(tf.dateCurr, '2012-10-29') as 'dateCurr' 
FROM dateFile df Full Outer join Contact ct 
On ct.numCode = df.numCode and df.dateCurr = '2012-10-29' 
+0

非常感謝。這項工作;) – nettoon493

0

試試這個:

select c.numCode,ISNULL(d.dateCurr,'2012-10-29 00:00:00.000') from Contact c left join dateFile d 
on c.numCode = d.numCode