您好,我想更新我的資料,我的資料如下所示:
list = {sad,dfgdf,vxcvxcv}
我在 postgres 中使用了資料型別 text[] 。我想將例如poc添加到這個陣列 ("{sad,dfgdf,vxcvxcv}") 如果它不包含 poc 并且如果 poc 已經在該陣列中,我不想更新它。有沒有辦法做到這一點?
uj5u.com熱心網友回復:
使用@>運算子檢查陣列是否包含“poc”,array_append如果不包含則添加它。這是一個插圖。
with t(x, y, arr) as
(
values
('testa', 'BH06', '{sad,dfgdf,vxcvxcv}'::text[]),
('testb', 'BH07', '{das,fdgfd,abcbabc,poc}'),
('testc', 'BH08', '{}')
)
select x, y,
case when not arr @> '{poc}' then array_append(arr, 'poc') else arr end arr
from t;
結果:
| X | 是 | 阿爾 |
|---|---|---|
| 種皮 | BH06 | {悲傷,dfgdf,vxcvxcv,poc} |
| 測驗b | BH07 | {das,fdgfd,abcbabc,poc} |
| 測驗 | BH08 | {poc} |
有關陣列操作的詳細資訊,請參見此處。請注意,在 Postgresql 中,名稱使用雙引號,字串文字使用單引號。
更新查詢,假設表中的目標列被稱為arr:
update the_table
set arr = array_append(arr, 'poc')
where not arr @> '{poc}';
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/332885.html
標籤:sql 数据库 PostgreSQL
上一篇:android房間關系:缺少列
下一篇:R:根據有序向量獲取資料幀的值
