日期:2014-05-17 浏览次数:20465 次
create table #t1(ID int, NAME varchar(10),LESSON varchar(10))
insert into #t1 values(1,'b','1,2,3')
insert into #t1 values(2,'bb','4')
insert into #t1 values(3,'bbb','5')
go
create table #t2(ID int, NAME varchar(10))
insert into #t2 values(1, 'aa')
insert into #t2 values(2, 'aaa')
insert into #t2 values(3, 'aaaa')
go
select t1.*,
(select t.NAME+','
from #t2 t
where ','+CAST(t1.LESSON as varchar)+',' like '%,'+cast(t.ID as varchar)+',%'
for xml path('')) as LESSONs
from #t1 t1
drop table #t1
drop table #t2
create table #t1(ID int, NAME varchar(10),LESSON varchar(10))
insert into #t1 values(1,'b','1,2,3')
insert into #t1 values(2,'bb','4')
insert into #t1 values(3,'bbb','5')
go
create table #t2(ID int, NAME varchar(10))
insert into #t2 values(1, 'aa')
insert into #t2 values(2, 'aaa')