我得到加入 2 个表,但使用 3 个表。我不需要重复值。但我尝试加入有重复。
表peopleAll
:
pNo pName
-------------
00001 Sang
00002 Janta
00003 Els
00004 Est
00005 Sam
00006 John
00007 Misan
00008 Wila
00009 light
00010 Smith
00011 Ritpo
表fTime
:
cNo cDate cIn cOut
-----------------------------------------------------------------------------------
00001 2012-10-22 00:00:00.000 2012-10-22 07:59:00.000 2012-10-22 20:34:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 12:50:00.000 2012-10-22 19:50:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 12:01:00.000 2012-10-22 20:30:00.000
00004 2012-10-22 00:00:00.000 2012-10-22 07:55:00.000 2012-10-22 20:30:00.000
00005 2012-10-22 00:00:00.000 2012-10-22 07:27:00.000 2012-10-22 20:31:00.000
00010 2012-10-22 00:00:00.000 2012-10-22 07:12:00.000 2012-10-22 20:22:00.000
表Leave
:
lNo lDate lStart lStop
-----------------------------------------------------------------------------------
00002 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00011 2012-10-22 00:00:00.000 NULL NULL
此 SQL 代码:
SELECT lr.lNo, lr.lDate, lr.lStart, lr.lStop
FROM Leave lr
Where lr.cStart = '2012-10-22'
UNION ALL
SELECT pa.pNo, ISNULL(tf.cDate, Convert(nvarchar(10),'2012-10-22',114)),tf.cIn, tf.cOut
FROM fTime tf FULL OUTER join peopleAll pa On tf.cNo = pa.pNo AND
tf.cDate = Convert(nvarchar(10),'2012-10-22',114)
返回此输出:
lNo lDate lStart lStop
-----------------------------------------------------------------------------------
00001 2012-10-22 00:00:00.000 2012-10-22 07:59:00.000 2012-10-22 20:34:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 12:50:00.000 2012-10-22 19:50:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 12:01:00.000 2012-10-22 20:30:00.000
00004 2012-10-22 00:00:00.000 2012-10-22 07:55:00.000 2012-10-22 20:30:00.000
00005 2012-10-22 00:00:00.000 2012-10-22 07:27:00.000 2012-10-22 20:31:00.000
00006 2012-10-22 00:00:00.000 NULL NULL
00007 2012-10-22 00:00:00.000 NULL NULL
00008 2012-10-22 00:00:00.000 NULL NULL
00009 2012-10-22 00:00:00.000 NULL NULL
00010 2012-10-22 00:00:00.000 2012-10-22 07:12:00.000 2012-10-22 20:22:00.000
00011 2012-10-22 00:00:00.000 NULL NULL
FULL OUTER JOIN
然后我尝试更改LEFT OUTER JOIN
并得到输出:
lNo lDate lStart lStop
-----------------------------------------------------------------------------------
00002 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 12:50:00.000 2012-10-22 19:50:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 08:00:00.000 2012-10-22 12:00:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 12:01:00.000 2012-10-22 20:30:00.000
00004 2012-10-22 00:00:00.000 2012-10-22 07:55:00.000 2012-10-22 20:30:00.000
00007 2012-10-22 00:00:00.000 NULL NULL
00008 2012-10-22 00:00:00.000 NULL NULL
00010 2012-10-22 00:00:00.000 2012-10-22 07:12:00.000 2012-10-22 20:22:00.000
00011 2012-10-22 00:00:00.000 NULL NULL
LEFT OUTER join 错过了一些字段??
我需要这个输出:
lNo lDate lStart lStop
-----------------------------------------------------------------------------------
00001 2012-10-22 00:00:00.000 2012-10-22 07:59:00.000 2012-10-22 20:34:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 12:50:00.000 2012-10-22 19:50:00.000
00003 2012-10-22 00:00:00.000 2012-10-22 12:01:00.000 2012-10-22 20:30:00.000
00004 2012-10-22 00:00:00.000 2012-10-22 07:55:00.000 2012-10-22 20:30:00.000
00005 2012-10-22 00:00:00.000 2012-10-22 07:27:00.000 2012-10-22 20:31:00.000
00006 2012-10-22 00:00:00.000 NULL NULL
00007 2012-10-22 00:00:00.000 NULL NULL
00008 2012-10-22 00:00:00.000 NULL NULL
00009 2012-10-22 00:00:00.000 NULL NULL
00010 2012-10-22 00:00:00.000 2012-10-22 07:12:00.000 2012-10-22 20:22:00.000
00011 2012-10-22 00:00:00.000 NULL NULL
(00002,00003 from fTime)
请帮我。谢谢你的时间。:)