Array_agg in postgres selectively quotes

前端 未结 2 756
走了就别回头了
走了就别回头了 2021-01-26 22:15

I have a complex database with keys and values stored in different tables. It is useful for me to aggregate them when pulling out the values for the application:



        
相关标签:
2条回答
  • 2021-01-26 22:58

    Can't you use json_agg?

    select json_agg(txt) from test;
                   json_agg               
    --------------------------------------
     ["one", "two", "three", "four five"]
    
    0 讨论(0)
  • 2021-01-26 23:12

    Unfortunately, this is the inconsistent standard that PostgreSQL uses for formatting arrays. See "Array Input and Output Syntax" for more information.

    Clodoaldo's answer is probably what you want, but as an alternative, you could also build your own result:

    SELECT '{'||array_to_string(array_agg(txt::text), ',')||'}' FROM test;
    
    0 讨论(0)
提交回复
热议问题