首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

不一般基础~关于包含查询求解

2012-09-22 
特别基础~关于包含查询求解~表1里的字段id:name:1孙悟空2白骨精表2里的字段content:孙悟空三打白骨精白骨

特别基础~关于包含查询求解~
表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*/不知道是不是你要的。 

热点排行
Bad Request.