表
a b
1 2
2 2
3 2
4 2
5 2
where b=2 查询后结果为
1,2,3,4,5
SQL code:
select stuff((select a from tb where b=2 order by a for xml path('')),1,1,'')
SQL code:
declare @s varchar(8000)
set @s=''
select @s=@s+a+',' from tb where b=2
SQL code:
declare @s varchar(100)
select @s=isnull(@s+',','')+ltrim(a)from tb where b=2
SQL code:
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([a] int,[b] int)
go
insert [tb]
select 1,2 union all
select 2,2 union all
select 3,2 union all
select 4,2 union all
select 5,2
select stuff((select ','+ltrim(a) from tb where b = t.b for xml path('')),1,1,'' )
from [tb] t
where b = 2
group by b
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id url rank ......