ID
1
2
3
我只知道 select ID from A输出的是多条记录,如何输出这样的形式1,2,3
谢谢大家。
5 个解决方案
#1
WITH CTE AS (SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
)
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM CTE WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM CTE t
GROUP BY t.dah
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
)
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM CTE WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM CTE t
GROUP BY t.dah
#2
declare @test table( name varchar(10))
insert into @test values('a'),('b'),('c'),('d');
select distinct (select cast(name as varchar(2))+'*'from @test for xml path(''))as name from @test
一条语句将一个字段数据换转成一个字符串
insert into @test values('a'),('b'),('c'),('d');
select distinct (select cast(name as varchar(2))+'*'from @test for xml path(''))as name from @test
一条语句将一个字段数据换转成一个字符串
#3
CREATE TABLE tb(dah VARCHAR(10),fid int)
INSERT tb
SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM tb WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM tb t
GROUP BY dah
INSERT tb
SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM tb WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM tb t
GROUP BY dah
#4
declare @s varchar(8000) set @s=''
select @s=@s+name+','
from syscolumns
where id=object_id('表名')
print left(@s,len(@s)-1)
select @s=@s+name+','
from syscolumns
where id=object_id('表名')
print left(@s,len(@s)-1)
#5
create table tb(id varchar(10))
insert into tb values(1)
insert into tb values(2)
insert into tb values(3)
declare @s varchar(8000) set @s=''
select @s=@s+id+','
from tb
print left(@s,len(@s)-1)
drop table tb
/*
1,2,3
*/
insert into tb values(1)
insert into tb values(2)
insert into tb values(3)
declare @s varchar(8000) set @s=''
select @s=@s+id+','
from tb
print left(@s,len(@s)-1)
drop table tb
/*
1,2,3
*/
#1
WITH CTE AS (SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
)
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM CTE WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM CTE t
GROUP BY t.dah
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
)
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM CTE WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM CTE t
GROUP BY t.dah
#2
declare @test table( name varchar(10))
insert into @test values('a'),('b'),('c'),('d');
select distinct (select cast(name as varchar(2))+'*'from @test for xml path(''))as name from @test
一条语句将一个字段数据换转成一个字符串
insert into @test values('a'),('b'),('c'),('d');
select distinct (select cast(name as varchar(2))+'*'from @test for xml path(''))as name from @test
一条语句将一个字段数据换转成一个字符串
#3
CREATE TABLE tb(dah VARCHAR(10),fid int)
INSERT tb
SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM tb WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM tb t
GROUP BY dah
INSERT tb
SELECT
'A' dah,10 fid UNION SELECT
'A',11 UNION SELECT
'A',12 UNION SELECT
'B',20 UNION SELECT
'B',21 UNION SELECT
'B',22 UNION SELECT
'C',30 UNION SELECT
'C',31
SELECT dah,fid=STUFF((SELECT ','+CAST(fid AS VARCHAR(10)) FROM tb WHERE dah=t.dah FOR XML PATH('')),1,1,'')
FROM tb t
GROUP BY dah
#4
declare @s varchar(8000) set @s=''
select @s=@s+name+','
from syscolumns
where id=object_id('表名')
print left(@s,len(@s)-1)
select @s=@s+name+','
from syscolumns
where id=object_id('表名')
print left(@s,len(@s)-1)
#5
create table tb(id varchar(10))
insert into tb values(1)
insert into tb values(2)
insert into tb values(3)
declare @s varchar(8000) set @s=''
select @s=@s+id+','
from tb
print left(@s,len(@s)-1)
drop table tb
/*
1,2,3
*/
insert into tb values(1)
insert into tb values(2)
insert into tb values(3)
declare @s varchar(8000) set @s=''
select @s=@s+id+','
from tb
print left(@s,len(@s)-1)
drop table tb
/*
1,2,3
*/