我是postgres的新手,
我试图通过在posgres中使用json对象连接表来实现多个更新。
我有一张桌子" timestamp_snapshot"以下数据..
channel_id(varchar) date_in (timestamp)
"sam_channel" "2016-06-27 19:36:40.706018"
"vam_channel" "2016-06-27 19:36:40.706018"
我有一个json对象,如下所示
'{ "sam_channel" : "2016-06-27T19:36:40.706018", "vam_channel" : "2016-06-29T19:42:34.812616" }'
我想更新表" timestamp_snapshot"的date_in列。使用json对象的数据
更新后的输出,我想在下面给出:
channel_id(varchar) date_in (timestamp)
"sam_channel" "2016-06-27 19:36:40.706018"
"vam_channel" "2016-06-29 19:42:34.812616"
我用下面的方法尝试了这个问题,但表格中的数据" timestamp_snapshot"在更新后仍保持不变。
我的方法:
update timestamp_snapshot
set date_in = latest_snap.value
from
timestamp_snapshot as tab_time_snap,
(
select j.key, j.value::timestamp
from json_each_text('{ "sam_channel" : "2016-06-27T19:36:40.706018",
"vam_channel" : "2016-06-29T19:42:34.812616" }') j
) latest_snap
where latest_snap.key = tab_time_snap.channel_id
我发现他们的查询存在问题,因为我是postgres的新手,我非常感谢您的帮助。
答案 0 :(得分:1)
这可能是您引用要更新两次的表的结果。如果您将声明更改为此,您是否得到了预期的结果?:
UPDATE timestamp_snapshot
SET date_in = latest_snap.value
FROM
(
SELECT j.key, j.value::timestamp
FROM json_each_text('{ "sam_channel" : "2016-06-27T19:36:40.706018",
"vam_channel" : "2016-06-29T19:42:34.812616" }') j
) latest_snap
WHERE timestamp_snapshot.channel_id = latest_snap.key;
参考文档:https://www.postgresql.org/docs/9.5/static/sql-update.html