日期:2014-05-16 浏览次数:20839 次
--测试数据
create table tb2
(
name varchar(50)
)
insert into tb2 select 'aa';
insert into tb2 select 'bb';
insert into tb2 select 'aa';
insert into tb2 select 'cc';
insert into tb2 select 'dd';
-- 正常去重复语句
select distinct name from tb2;
/*
----------
name
aa
bb
cc
dd
----------
*/
select distinct ','+name from tb2;
/*
-------------
,aa
,bb
,cc
,dd
-------------
*/
--本人现在更具需求想获取一个列表所以按照下面的写了但是结果很让人不解--求解答
declare @List nvarchar(600);
set @List='';
select distinct @List=@List+','+name
from tb2
print @List;
/*
-----------
,dd
-----------
*/
declare @List nvarchar(600);
set @List='';
select @List=@List+','+a.name
from (select distinct name from tb2) a
print @List;
/*
,aa,bb,cc,dd
*/
--测试数据
create table tb2
(
name varchar(50)
)
insert into tb2 select 'aa';
insert into tb2 select 'bb';
insert into tb2 select 'aa';
insert into tb2 select 'cc';
insert into tb2 select 'dd';
go
declare @List nvarchar(600);
set @List='';
select @List=@List+','+name
from tb2
group by name
select @List
/*
,aa,bb,cc,dd
*/