日期:2014-05-18  浏览次数:20852 次

SQL 查询 如何得出结果
表wan
A         B         C
1         你       1
1         好       2
1         啊       3
2         我       1
2         爱       2
2         你       3


如上表,SQL   查询   如何得出结果   如
1         你好啊
2         我爱你

------解决方案--------------------
create table test(A int, B char(2),C int)
insert test select 1 , '你 ', 1
union all select 1 , '好 ', 2
union all select 1 , '啊 ', 3
union all select 2 , '我 ', 1
union all select 2 , '爱 ', 2
union all select 2 , '你 ', 3
go

create function getJJ(@i int)
returns varchar(100)
as
begin
declare @a varchar(100)
select @a=isnull(@a+ ' ', ' ')+ b from test where a=@i order by c
return @a
end
go

select a,dbo.getjj(a) from test group by a