如何解决Postgres:将jsonb转换为jsonb []?
假设我有这张桌子:
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_info
表中的所有行中提取player
并将其插入report
表中的一行(插入data
{{ 1}}字段)?我对jsonb[]
的尝试返回了jsonb_agg()
,但我终生无法弄清楚如何从jsonb
到jsonb
。任何指针将不胜感激!预先感谢。
解决方法
如果您明确想要复制值,则将其与其他任何数据类型一样对待,并使用ARRAY_AGG
。
SELECT ARRAY_AGG(player_info)
FROM player
WHERE id IN (...)
应返回json[]
类型的内容。
由于jsonb[]
是PostgreSQL中类型级别的数组,而不是json数组,因此请使用array_agg()
而不是jsonb_agg()
。
insert into report
select 1 as id,now() as created,array_agg(player_info)
from player
;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。