Alternatives to array_agg()?

只谈情不闲聊 提交于 2019-12-23 04:43:50

问题


Is there any alternative to the PostgreSQL array_agg() function so that it doesn't return values in the format: '{x,y,z,}'.
Can I have it return just: 'x,y,z' ?


回答1:


In PostgreSQL 9.0 or later use string_agg(val, ',').
It returns a string with delimiters of your choosing.

array_agg(val) returns an array, no surprise there. The curly braces you see are integral part of array literals - the text representation of arrays.

In older versions (or any version really) you can substitute with array_to_string(array_agg(val), ',').

Or, quick'n'dirty: trim(array_agg(val)::text, '{}' - if values never start or end with curly braces.




回答2:


If you are not on 9.0 yet (which has the already mentioned string_agg() function) you can use array_to_string() on the result of array_agg()




回答3:


Use the STRING_AGG function:

SELECT
  STRING_AGG(name, ',')
FROM
  person;


来源:https://stackoverflow.com/questions/9524994/alternatives-to-array-agg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!