--> 测试数据:[iTarget_BrandOption]
go
if object_id('[iTarget_BrandOption]') is not null
drop table [iTarget_BrandOption]
go
create table [iTarget_BrandOption](
[BrandID] int,
[BrandName] varchar(3)
)
go
insert [iTarget_BrandOption]
select 1,'Bud'
--> 测试数据:[iTarget_PackageOption]
go
if object_id('[iTarget_PackageOption]') is not null
drop table [iTarget_PackageOption]
create table [iTarget_PackageOption](
[PackageID] int,
[PackageName] varchar(3),
[BrandID] int
)
go
insert [iTarget_PackageOption]
select 1,'BBT',1 union all
select 2,'SBT',1 union all
select 3,'CAN',1 union all
select 4,'OTH',1
;with t
as(
select
a.BrandName as name1,b.PackageName as name2
from [iTarget_BrandOption] a
inner join [iTarget_PackageOption] b
on a.BrandID=b.BrandID
)
select distinct name1 from t
union all
select name2 from t
where name1 in(select name1 from t a where t.name1=a.name1)