合并相同id的字符串.sql

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

SQL
44
字号
ID    COTENT--------------------1  , "aaa"1  , "bbb"1  , "ccc"2  , "aa "2  , "bb"  如何写sql语句让相同的id的content合并 就是结果变成ID  Cotent-------------------------1 ,"aaabbbccc"2 ,"aabb" declare @a table(id int ,cotent varchar(10))insert into @a select 1,'"aaa"'union all     select 1,'"bbb"'union all     select 1,'"ccc"'union all     select 2,'"aa"'union all     select 2,'"bb"'declare @b table (autoid int identity(1,1),id int,cotent varchar(10))insert into @b select * from @adeclare @aa int,@bb int,@cc  varchar(20)select @aa=(select min(id) from @b)select @bb=1while @aa<=(select max(id) from @b) begin    select @cc=(select replace(cotent,'"','') from @b where id=@aa and autoid=@bb)    declare @c table(autoid int identity(1,1),id int,cotent varchar(20))    while @bb<=(select max(autoid) from @b where id=@aa)      begin      insert into @c select @aa,@cc      select @bb=@bb+1      select @cc=@cc+(select replace(cotent,'"','') from @b where id=@aa and autoid=@bb)      end select @aa=(select min(id) from @b where id>@aa) endselect id,'"'+cotent+'"'as Cotent from @c where autoid in (select autoid from (select id,max(autoid)as autoid from @c  group by id)a)

⌨️ 快捷键说明

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