我搜到两行数据
id name sex num
1 j 0 部门
1 j 0 机构
我想得到这样的结果
id name sex num
1 j 0 部门,机构
求大神教我啊,我用的是sqlserver2000
这个 sql 写不出来把! 存储过程是可以的。。。。
SELECT t.id,t.name,t.sex,GROUP_CONCAT(t.num) from test t GROUP BY t.id,t.name,t.sex
mysql可以用GROUP_CONCAT函数,sqlserver好像要自己写
//去重,加上distinct
select distinct id ,name ,sex,num from table_name
sql server 不支持XML PATH
你可以定义一个Function
create table tb(id int, name varchar(10), sex int, num nvarchar(10))
go
insert into tb
select 1,'j',0,N'部门' union all
select 1,'j',0,N'机构'
go
create function fn_concat_num(@id int)
returns nvarchar(4000)
begin
declare @s nvarchar(4000)
select @s=isnull(@s+N',','')+num from tb where id=@id
return @s
end
go
select distinct ID,dbo.fn_concat_num(1) from tb