簡體   English   中英

Postgresql:用另一列的值更新文本列

[英]Postgresql:Update text column with value from another column

我是 postgresql 的新手,需要更新腳本的幫助,如果沒有,我可以在路徑2 和文件名之間插入相應的 ID。這將在多條記錄上完成。

table1  
id       name     path
111      Test1    /path1/path2/file1.png
222      Test1    /path1/path2/222/file2.png
333      Test3    /path1/path2/file3.png
444      Test3    https:test/path1/path2/file4.png
555      Test4    /path1/path2/file5.png

更新后,只有 ID 111 和 333 會受到影響,預期為 /path1/path2/111/file1.png /path1/path2/333/file3.png

我正在使用這個 select 來獲取需要更新的記錄。

select * from table1
where name in ('Test1','Test3')
and path like '/path1/path2/%'
and path not like '/path1/path2/%/%'

請試試這個:

update table1 
   set path = replace(path, '/path2/', concat('/path2/', id::text, '/'))
 where path !~ concat('/path2/', id::text, '/');
UPDATE 4

select * from table1;

 id  | name  |                 path                 
-----+-------+--------------------------------------
 222 | Test1 | /path1/path2/222/file2.png
 111 | Test1 | /path1/path2/111/file1.png
 333 | Test3 | /path1/path2/333/file3.png
 444 | Test3 | https:test/path1/path2/444/file4.png
 555 | Test4 | /path1/path2/555/file5.png
(5 rows)

暫無
暫無

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

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