日期:2014-05-18 浏览次数:20927 次
Create Table #A ( id int identity(1,1), AName nvarchar(50), BName nvarchar(50) ) Create Table #B ( id int identity(1,1), AName nvarchar(50), BName nvarchar(50) ) insert into #A select 'A1','A1' union select 'A2','A2' union select 'A3','A3' union select 'A4','A4' union select 'A5','A5' union select 'A6','A6' union select 'A7','A7' insert into #B select 'A1','A1' union select 'A2','A2' union select 'A3','A3' union select 'A4','A4' union select 'A5','A5' union select 'A8','A8' union select 'A9','A9' SELECT c.*, case isnull(a.id,'') When '' Then '无' ELSE '有' END A, case isnull(b.id,'') When '' Then '无' ELSE '有' END B FROM (Select * from #A union SELECT * FROM #B) c LEFT JOIN #A a ON a.AName=c.AName AND a.BName=c.BName LEFT JOIN #B b ON b.AName=c.AName AND b.BName=c.BName DROP TABLE #A DROP TABLE #B 1 A1 A1 有 有 2 A2 A2 有 有 3 A3 A3 有 有 4 A4 A4 有 有 5 A5 A5 有 有 6 A6 A6 有 无 6 A8 A8 无 有 7 A7 A7 有 无 7 A9 A9 无 有