在psql中,将来自不同表的数据选择插入到jsonb列中

3

在psql中,将不同表中的数据插入到一个jsonb列中。我希望jsonb插入如下:

{"name": "myname" ,"email": "test@gmail.com"}

我想做类似于这样的一件事情:"name":"myname"是一个常量值,并且电子邮件是从另一张表中选择的。
insert into test1 (column1) select {"name": "myname" ,"email": email}
1个回答

3
只需使用row_to_json将所选行转换为json。然后根据需要将其转换为jsob。"最初的回答"
insert into test1 (column1)
select row_to_json(x)::jsonb from (select 'myname' as name, email from another_table) x;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接