表Lable如上
现在按照LableTypeId分组,然后将LableId拼接起来:效果如下:
代码:
select LableTypeId,
STUFF((select ',' + convert(varchar(50),LableId) from L_Lable where LableId in (1,2,3,6,7) and LableTypeId = a.LableTypeId for xml path('')),1,1,'') as ids
from L_Lable a where LableId in (1,2,3,6,7) group by LableTypeId
convert(varchar(50),LableId) :int转varchar(50)
STUFF ( character_expression , start , length ,character_expression )
character_expression:字符数据
start :指定删除和插入的开始位置
length :指定要删除的字符数
character_expression : 替换start 到 length 的字符数据