declare @t table(id int,name varchar(10),sex char(1))
insert @t
select 1, 'aa', 'm'
union all
select 2, 'bb', 'f'
union all
select 3, 'cc', 'f'
declare @t1 table(id int,name varchar(10),value varchar(10))
insert @t1
select 1, 'ad','ad1'
union all
select 1, 'age', 'ag1'
union all
select 2, 'ad', 'ad2'
union all
select 2, 'age', 'ag2'
union all
select 3, 'ad', 'ad3'
union all
select 3, 'age', 'ag3'
select distinct a.*,(select value from @t1 where id=a.id and name='ad') as ad,(select value from @t1 where id=a.id and name='age') as age
from @t a left join @t1 b on a.id=b.id
你说的合并是列合并,还是行合并?
如果是行合并,也就是数据集合合并用union就可以。
列合并即关联。用left|right outer join
例子:a/b/c 为结果集合
select * from (a union b union c) as t where t.*逻辑条件。
select * from a left outer b on a.关联字段名=b.关联字段名