连接重复字段(a;b;c).txt

来自「SQL语言常用的一些命令各代码」· 文本 代码 · 共 31 行

TXT
31
字号
题目:
数据库记录:
 1      A
 1      B
 1      C

写个sql语句要得到如下记录。
1   A;B;C

答案:
create table 面试1
(id int not null,
 jilu  char(1))
insert into 面试1 select 1,'A'
union all         select 1,'B'
union all         select 1,'C'

select * from 面试1

declare @temp table(a int identity(1,1), id int,jilu char(1))
insert into @temp(id,jilu)
select id,jilu from 面试1
declare @b int,@jilu varchar(50)
select @b=(select min(a) from @temp)
set @jilu=''
while @b<=(select max(a) from @temp where id=1)
begin
set @jilu=@jilu+(select jilu from @temp where a=@b)+';'
set @b=(select min(a) from @temp where a>@b)
end
print @jilu

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?