直接从JSON对象更新Postgresql表

时间:2017-02-16 12:41:48

标签: json postgresql

我用

select to_json(host) from host where id=3

从我的postgreSQL数据库查询数据结果如下:

{"id":3,"project_id":1,"name":"a","mac":"abc","info":"x"}

在我的应用程序中更改数据后,我想更新表。

这样做有“json”吗?没有做像

这样的普通更新
update host set project_id=1, name='a', mac='abc',info='x' where id=1;

1 个答案:

答案 0 :(得分:4)

jsonb_populate_record

中使用update from clause
update host
set
    (project_id, name, mac, info) =
    (j.project_id, j.name, j.mac, j.info)
from jsonb_populate_record ( null::host,
    '{"id":1,"project_id":1,"name":"a","mac":"abc","info":"x"}'::jsonb
) j
where host.id = j.id