SQL SERVER 2008, 如何将第一个表通过第二个表转换成第三个表,有逗号隔开了

 我来答
baiynijecym
2012-11-06 · TA获得超过1841个赞
知道大有可为答主
回答量:1411
采纳率:66%
帮助的人:1284万
展开全部
可如下
create table tab1(col1 int identity,col2 varchar(8));
create table tab2(col1 varchar(10) ,col2 varchar(8));

insert into tab1 (col2)
select 'ABD'
union all
select 'AE'

insert into tab2
select 'A','啊'
union all
select 'B','吧'
union all
select 'C','才'
union all
select 'D','的'
union all
select 'E','额'
;

select T1.col1,
STUFF((select ',' + tab2.col2 from (select col1,substring(col2,number,1) as col2 from tab1 a,master..spt_values b where b.number <= len(col2) and b.type = 'P' and b.number > 0) as T2 join tab2 on T2.col2 = tab2.col1 where T1.col1 = T2.col1 order by T2.col2 for xml path('')),1,1,'')
from
(select col1,substring(col2,number,1) as col2 from tab1 a,master..spt_values b where b.number <= len(col2) and b.type = 'P' and b.number > 0) as T1
group by T1.col1;
szm341
2012-11-06 · TA获得超过6727个赞
知道大有可为答主
回答量:5005
采纳率:100%
帮助的人:5581万
展开全部
select A

stuff((select '、'+t2.B from table1 t1 inner join table2 t2 on t2.A like '%'+t1.B+'%'
where t1.A=t.A for xml path('')),1,1,'') as B

from table1 t
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式