假设表a
id name
1 hello
2 jack
3 china
然后有个表b
str
hello,jack
china,
jack,china
如何实现把表b字段str中的字符串替换成表a中对应词语的id
表b最终变为
str
1,2
3
2,3
------解决方案--------------------
select [str]=stuff((select ','+ltrim(id)from a,b where charindex(','+a.name+',',','+b.name+',')>0for xml path('')), 1, 1, '') from b
------解决方案--------------------
select [str]=stuff((select ','+ltrim(id)from a,b where charindex(','+a.name+',',','+b.name+',')>0 for xml path('')), 1, 1, '') from b