日期:2014-05-17  浏览次数:20725 次

求 一句sql语句 急急急
MYSQL数据库

表user  字段:id,sid,liuyan
++++++++++++++++++++++++
id  sid  liuyan
1    3    很好的
2    3    什么东东
3    3    要下雨了
4    4    我的书啊
5    5    看什么的电影
6    3    吃饭不管事
7    4    眼睛听话
8    5    不要说话
9    7    风雨无阻
10   9    来了去了
11   8    不是不是
12   7    欧洲杯歇了
13   9    不要管了

。。。。。。。

要求 输出效果:
id  sid  liuyan
1    3   很好的,什么东东,要下雨了,吃饭不管事
2    4   我的书啊,眼睛听话
3    5   看什么的电影,不要说话
4    7   风雨无阻,欧洲杯歇了
5    8   不是不是
6    9   来了去了,不要管了

求一句sql语句,也就是 group by sid 并且把liuyan合并显示。

------解决方案--------------------
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,'')