34,590
社区成员
发帖
与我相关
我的任务
分享
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([ID] int,[Name] nvarchar(22),[Type] nvarchar(21))
Insert #A
select 1,N'AA',N'是' union all
select 2,N'AA',N'否'
GO
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([Name] nvarchar(22),[FullName] nvarchar(23))
Insert #B
select N'AA',N'测试A'
GO
if not object_id(N'Tempdb..#C') is null
drop table #C
Go
Create table #C([Name] nvarchar(22),[FullName] nvarchar(23))
Insert #C
select N'AA',N'测试B'
Go
select A.*,t.FullName from #a a
left join
(select *,'是' as tp from #b
union all
select *,'否' as tp from #c
)t
on a.Name =t.Name and a.Type =t.tp
ID Name Type FullName
1 AA 是 测试A
2 AA 否 测试B
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([ID] int,[Name] nvarchar(22),[Type] nvarchar(21))
Insert #A
select 1,N'AA',N'是' union all
select 2,N'AA',N'否'
GO
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([Name] nvarchar(22),[FullName] nvarchar(23))
Insert #B
select N'AA',N'测试A'
GO
if not object_id(N'Tempdb..#C') is null
drop table #C
Go
Create table #C([Name] nvarchar(22),[FullName] nvarchar(23))
Insert #C
select N'AA',N'测试B'
Go
--测试数据结束
SELECT #A.* ,
CASE WHEN Type='是' THEN #B.FullName
ELSE #C.FullName
END AS FullName
FROM #A
LEFT JOIN #B ON #B.Name = #A.Name
LEFT JOIN #C ON #C.Name = #A.Name