22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[sid] int,[liuyan] varchar(12))
insert [tb]
select 1,3,'很好的' union all
select 2,3,'什么东东' union all
select 3,3,'要下雨了' union all
select 4,4,'我的书啊' union all
select 5,5,'看什么的电影' union all
select 6,3,'吃饭不管事' union all
select 7,4,'眼睛听话' union all
select 8,5,'不要说话' union all
select 9,7,'风雨无阻' union all
select 10,9,'来了去了' union all
select 11,8,'不是不是' union all
select 12,7,'欧洲杯歇了' union all
select 13,9,'不要管了'
go
select row_number() over(order by getdate()) as id,
sid,
liuyan=stuff((select ','+liuyan from tb where sid=t.sid for xml path('')),1,1,'')
from tb t
group by sid
/*
id sid liuyan
-------------------- ----------- -----------------------------------------------
1 3 很好的,什么东东,要下雨了,吃饭不管事
2 4 我的书啊,眼睛听话
3 5 看什么的电影,不要说话
4 7 风雨无阻,欧洲杯歇了
5 8 不是不是
6 9 来了去了,不要管了
(6 行受影响)
*/