我有一个文本数组列,现在我想创建一个新的 JSON 列。在 JSON 列中,我想从文本数组和当前时间戳创建键值对。
例子:
文本数组{"aa", "bb", "cc"}
-> JSON{"aa":"12:00", "bb":"12:00", "cc":"12:00"}
如何以这种方式更新整个表中的所有行?
假设当前列已命名data
并且新列是new_data
您可以执行以下操作:
update the_table
set new_data = x.new_data
from (
select id, jsonb_object_agg(t.k, to_char(now(), 'hh24:mi')) as new_data
from the_table, unnest(data) as t(k)
group by id
) x
where x.id = foo.id;
在线示例:https : //rextester.com/SMBH72985
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句