首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postgres: jsonb to jsonb[]?

Postgres: jsonb to jsonb[]?
EN

Stack Overflow用户
提问于 2020-08-14 23:59:38
回答 2查看 30关注 0票数 0

假设我有这个表:

代码语言:javascript
复制
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 |

然后我有了这个:

代码语言:javascript
复制
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[]。如果有任何建议,我们将不胜感激!提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-15 00:03:39

如果您显然想要复制值,只需像对待任何其他数据类型一样对待它,并使用ARRAY_AGG

代码语言:javascript
复制
SELECT ARRAY_AGG(player_info)
FROM player
WHERE id IN (...)

应该返回json[]类型的内容。

票数 2
EN

Stack Overflow用户

发布于 2020-08-15 00:03:49

由于jsonb[]是PostgreSQL中的类型级别的数组,而不是json数组,因此请使用array_agg()而不是jsonb_agg()

代码语言:javascript
复制
insert into report 
select 1 as id, now() as created, array_agg(player_info)
  from player
;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63416210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档