简体   繁体   English

Postgres:如何将文本数组列中的元素复制到json列中?

[英]Postgres: How to copy elements from text array column into a json column?

I have a text array column and now I would like to create a new JSON column. 我有一个文本数组列,现在我想创建一个新的JSON列。 In JSON column I would like to create key-value pairs from text array and current time stamp. 在JSON列中,我想根据文本数组和当前时间戳创建键值对。

Example: 例:

Text array {"aa", "bb", "cc"} -> JSON {"aa":"12:00", "bb":"12:00", "cc":"12:00"} 文本数组{"aa", "bb", "cc"} -> JSON {"aa":"12:00", "bb":"12:00", "cc":"12:00"}

How do I update this way all rows in the whole table? 如何以这种方式更新整个表中的所有行?

Assuming the current column is named data and the new column is new_data you can do something like this: 假设当前列的名称为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;

Online example: https://rextester.com/SMBH72985 在线示例: https//rextester.com/SMBH72985

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM