gpt4 book ai didi

arrays - Postgresql 使用 array_to_string 将字符串放在引号内

转载 作者:行者123 更新时间:2023-11-29 12:35:18 26 4
gpt4 key购买 nike

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

array_to_string(array_agg(tag_name),';') tag_names

我得到了结果字符串 "tag1;tag2;tag3;..." 但我想得到结果字符串 "'tag1';'tag2';'tag3'; ...”

我如何在 Postgres 中执行此操作?

最佳答案

使用函数string_agg()format()使用 %L 占位符,将参数值作为 SQL 文字引用。

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

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

tag_names
----------------------
'tag1';'tag2';'tag3'
(1 row)

或者,format('%L', tag_name) 可以替换为 quote_nullable(tag_name)

关于arrays - Postgresql 使用 array_to_string 将字符串放在引号内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50020223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com