postgresql:字符串求和(拼接)

解决方案:

1.将table转换为array

2.借助array_to_string将array进行拼接

 

示例:

--创建测试表
DROP TABLE IF EXISTS test;
CREATE TEMP TABLE test("Id" int,"Name" varchar(100),"Type" int);
INSERT INTO test("Id","Name","Type")VALUES(1,'NO1',1),(2,'NO2',2),(3,'NO3',2),(4,'NO4',2),(5,'NO5',3),(6,'NO6',3);

--将结果转换为数组
--SELECT array(SELECT "Name" FROM test)

--将数组合并为字符串
--select array_to_string(array[1,2,3], ',')

--合并后的结果
--SELECT array_to_string(array(SELECT "Name" FROM test), ',')

SELECT "Type" ,( array_to_string(array(SELECT "Name" FROM test as t WHERE t."Type" = test."Type" ) , ',') )  FROM test GROUP BY "Type";

DROP TABLE test;


 

    原文作者:PostgreSQL
    原文地址: https://www.cnblogs.com/zp900704/p/7193667.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞