如何在一个单元格中存储名称相同但id不同的值?

siv3szwd  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(384)
insert into demo values ('1','name1','Text1');
insert into demo values ('2','name1','Text2');
insert into demo values ('3','name1','Text3');

我怎么能这样吵架?

id | name  | value
1  | name1 | Text1 + Text2 + Text3
93ze6v8z

93ze6v8z1#

mysql数据库:
使用 GROUP_CONCAT 功能。

SELECT MIN(id), name, GROUP_CONCAT(DISTINCT Value SEPARATOR ' + ') AS Value
FROM demo
GROUP BY name

演示
http://sqlfiddle.com/#!9/d75aca/2号
mssql:

SELECT MIN(id), name,
       Value = STUFF((
          SELECT '+ ' + di.value + ' '
          FROM demo di
          WHERE d.name = di.name
          GROUP BY di.value
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM demo d
GROUP BY name

演示:
http://sqlfiddle.com/#!18/d75ac/2型

c6ubokkw

c6ubokkw2#

Oracle:

SELECT MIN(ID), NAME, LISTAGG(VALUE,'+') WITHIN GROUP (ORDER BY NAME) AS VALUE
FROM DEMO 
GROUP BY NAME

相关问题