Postgresql 将字符串放在带有 array_to_string 的引号内

dis*_*ame 2 arrays postgresql string-aggregation

在选择中,我使用了这样的 array_to_string(示例)

array_to_string(array_agg(tag_name),';') tag_names
Run Code Online (Sandbox Code Playgroud)

我得到了结果字符串,"tag1;tag2;tag3;..."但我想将结果字符串作为"'tag1';'tag2';'tag3';...".

我怎样才能在 Postgres 中做到这一点?

kli*_*lin 5

用途string_agg()format()功能,例如

with my_table(tag_name) as (
values 
    ('tag1'),
    ('tag2'),
    ('tag3')
)

select string_agg(format('''%s''', tag_name), ';' order by tag_name) tag_names
from my_table;

      tag_names       
----------------------
 'tag1';'tag2';'tag3'
(1 row)
Run Code Online (Sandbox Code Playgroud)