表1里的字段
id: name:
1 孙悟空
2 白骨精
表2里的字段
content:
孙悟空三打白骨精
白骨精勾引唐三藏
现在是想这么查数据:表2的content字段里如果包含表1的name字段的值,就把表1的id的值显示
求解~多谢~
------解决方案--------------------
select a.*,b.content from 表1 a,表2 b where b.content like '%'+a.name+'%'
------解决方案--------------------
- SQL code
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=1where 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,22 白骨精勾引唐三藏 2*/不知道是不是你要的。