我怎样才能使用全外连接?

时间:2012-10-29 09:06:54

标签: sql sql-server stored-procedures full-outer-join

我需要使用join 2 table显示所有数据。

考试

表格联系

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 Server

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

但我需要输出get:

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

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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