假设我有这个表:
ams=# \d player
Table "public.player"
Column | Type | Collation | Nullable | Default
-------------+--------------------------+-----------+----------+-------------------
id | integer | | not null |
created | timestamp with time zone | | not null | CURRENT_TIMESTAMP
player_info | jsonb | | not null |然后我有了这个:
ams=# \d report
Table "public.report"
Column | Type | Collation | Nullable | Default
---------+--------------------------+-----------+----------+---------
id | integer | | not null |
created | timestamp with time zone | | not null |
data | jsonb[] | | not null |如何从player表中的所有行中提取player_info并将其插入到report表的一行中(插入到data jsonb[]字段中)?我尝试用jsonb_agg()返回一个jsonb,但我永远也搞不懂如何从jsonb转到jsonb[]。如果有任何建议,我们将不胜感激!提前谢谢。
发布于 2020-08-15 00:03:39
如果您显然想要复制值,只需像对待任何其他数据类型一样对待它,并使用ARRAY_AGG。
SELECT ARRAY_AGG(player_info)
FROM player
WHERE id IN (...)应该返回json[]类型的内容。
发布于 2020-08-15 00:03:49
由于jsonb[]是PostgreSQL中的类型级别的数组,而不是json数组,因此请使用array_agg()而不是jsonb_agg()。
insert into report
select 1 as id, now() as created, array_agg(player_info)
from player
;https://stackoverflow.com/questions/63416210
复制相似问题