日期:2014-05-17 浏览次数:20443 次
declare @t1 table(id int,name varchar(10)); declare @t2 table(id int,content varchar(50)); insert into @t1 select 1,'孙悟空' union all select 2,'白骨精'; insert into @t2 select 1,'孙悟空三打白骨精' union all select 2,'白骨精勾引唐三藏'; ;with cte as ( select a.id,a.name,b.id as bid,b.content from @t1 a join @t2 b on 1=1 where b.content like '%'+a.name+'%' ) select a.bid,content,stuff((select ','+cast(b.id as varchar(10)) from cte b where b.bid=a.bid for xml path('')),1,1,'') as x from cte a group by a.bid,content /* bid content x ----------- -------------------------- ----------------- 1 孙悟空三打白骨精 1,2 2 白骨精勾引唐三藏 2 */ 不知道是不是你要的。