gpt4 book ai didi

arrays - postgres 中的 Array_agg 有选择地引用

转载 作者:行者123 更新时间:2023-11-29 12:32:36 27 4
gpt4 key购买 nike

我有一个复杂的数据库,其中的键和值存储在不同的表中。在提取应用程序的值时聚合它们对我很有用:

   SELECT array_agg(key_name), array_agg(vals)
FROM (
SELECT
id,
key_name,
array_agg(value)::VARCHAR(255) AS vals
FROM factor_key_values
WHERE id=20
GROUP BY key_name, id
) f;

这个特定的查询,在我的例子中给出了以下无效的 JSON:

-[ RECORD 1 ]-----------------------------------------------------------------------  
array_agg | {"comparison method","field score","field value"}
array_agg | {"{\"text category\"}","{100,70,50,0,30}","{A,B,C,F,\"No Experience\"}"}

请注意,仅当字符串有空格时才引用 varchars 数组。我已将其缩小到 ARRAY_AGG 的行为。为了完整起见,这里有一个例子:

BEGIN;
CREATE TABLE test (txt VARCHAR(255));
INSERT INTO test(txt) VALUES ('one'),('two'),('three'), ('four five');
SELECT array_agg(txt) FROM test;

结果将是:

{one,two,three,"four five"}

这就是我的 JSON 崩溃的原因。我可以处理应用程序代码中不带引号或带引号的字符串,但要混合使用。

有什么解决办法吗?

最佳答案

你不能用json_agg吗?

select json_agg(txt) from test;
json_agg
--------------------------------------
["one", "two", "three", "four five"]

关于arrays - postgres 中的 Array_agg 有选择地引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262126/

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