【发布时间】:2020-01-02 15:31:19
【问题描述】:
我有一个表 jsonb 表 t_1,其中包含 jsonb 类型的列 column_1,我需要转换为带有字符串的表,即 jsonb 值的连接
这是一个脚本
CREATE TABLE t_1 (
ID serial NOT NULL PRIMARY KEY,
column_1 jsonb NOT NULL
);
INSERT INTO t_1 (column_1)
VALUES
(
'{ "01": "Lily Bush", "03": "prod2uct"}'
),
(
'{ "41": "Josh William", "12": "product7"}'
),
(
'{ "07": "Mary Clark", "items" : "product2"}'
);
我试过了:
SELECT distinct jsonb_array_elements_text(column_1 -> jsonb_object_keys(column_1)) AS Actor FROM t_1
但是这个返回'无法从标量中提取元素'
我试过了:
SELECT tiket, string_agg(column_2, ', ') as list FROM(
SELECT column_1 ->> jsonb_object_keys(column_1) as column_2, id as tiket FROM t_1 ) as foo1
GROUP BY tiket
但这里是内部选择
如何在没有内部选择的情况下在一列中获取所有 jsonb 值,就像在第一个查询中一样?
我需要它在to_tsvector中使用
我需要用在
setweight(
to_tsvector('simple', column_with_json::text),
'A'
)
但是column_with_json::text 不是我需要的,我需要获取值,没有键
有什么例子吗?
【问题讨论】:
-
不要在 SELECT 列表中使用集合返回函数(如 jsonb_object_keys)。在
FROM部分使用它们 -
我需要在 setweight( to_tsvector('simple', column_with_json::text), 'A' ) 中使用它但是
column_with_json::text不是我需要的,我需要获取值,没有键任何例子?
标签: json postgresql aggregate-functions jsonb