【问题标题】:Postgres: edit values with regex?Postgres:用正则表达式编辑值?
【发布时间】:2011-05-16 21:00:45
【问题描述】:

我想在 postgres 中运行一个查询,查找文件路径为 cor/* 的表中的所有行并将它们设置为 con/*

在伪代码中:

UPDATE photo set filepath="con/*" where filepath="cor/*";

请任何人帮助我正确的 postgres 语法?这在 postgres 中可行吗?

非常感谢!

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    其实并不需要正则表达式:

    UPDATE photo
        SET filepath = 'con' || substring(filepath, 4)
    WHERE filepath LIKE 'cor/%'
    

    【讨论】:

    • 聪明的方法:)非常感谢。
    【解决方案2】:

    有一个 regexp_replace() 函数:

    http://www.postgresql.org/docs/current/static/functions-string.html

    update photo
    set filepath = regexp_replace(filepath, '^cor/', 'con/')
    where filepath ~ '^cor/';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 2020-09-29
      • 2014-07-23
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多