簡體   English   中英

Postgres:如何將文本數組列中的元素復制到json列中?

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

我有一個文本數組列,現在我想創建一個新的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM