12SELECT Customer_ID,Customer_Name,
class="baidu-highlight">STUFF "https://www.baidu.com/s?wd=PATH&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ujRkn1R1uW9hryN- njF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHm1rH04rH6snjnkn1mYnjR3n0" target="_blank" class="baidu-highlight">PATH
把表名tt换下就可以了购买产品id是字符串类型的话用这个select a.客户id,stuff((select ','+购买产品id from tt b wherea.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a group by a.客户id
如果购买产品id是整型的话,用这个select a.客户id,stuff((select ','+cast(购买产品id as nvarchar(max)) from tt b wherea.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a group by a.客户id
参考资料;百度百科-sql
--不用那么麻烦 用这个 把表名tt换下就可以了
--你的购买产品id是字符串类型的话用这个
select a.客户id,stuff((select ','+购买产品id from tt b where
a.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a
group by a.客户id
--如果购买产品id是整型的话,用这个
select a.客户id,stuff((select ','+cast(购买产品id as nvarchar(max)) from tt b where
a.客户id=b.客户id for XML path('') ),1,1,'') gg from tt a
group by a.客户id
这可能要用存储过程,先创建两个表,一个ID,一个你要用的结果表,然后对ID表用游标,将购买ID的列加出来,然后插入到你的结果集