首页 > 解决方案 > 插入到从 psql 中的不同表中选择的 jsonb 列中

问题描述

插入从 psql 中的不同表中选择的 jsonb 列。我想要jsonb插入

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

我想做一些类似“name”的事情:“myname”常量值和电子邮件是从另一个表中选择的

insert into test1 (column1) select {"name": "myname" ,"email": email}

标签: sqljsonpostgresqlsql-insert

解决方案


只需使用 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;

推荐阅读