【问题标题】:Postgresql:Update text column with value from another columnPostgresql:使用另一列的值更新文本列
【发布时间】:2020-07-03 12:49:39
【问题描述】:

我是 postgresql 新手,需要更新脚本方面的帮助,如果没有,我可以在 path2 和文件名之间插入相应的 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 * from table1
where name in ('Test1','Test3')
and path like '/path1/path2/%'
and path not like '/path1/path2/%/%'

【问题讨论】:

    标签: sql postgresql sql-update


    【解决方案1】:

    请试试这个:

    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)
    
    

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2014-11-25
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多