【问题标题】:MySql - Way to update portion of a string?MySql - 更新字符串部分的方法?
【发布时间】:2010-12-25 00:11:34
【问题描述】:

我正在寻找一种通过 MySQL 查询仅更新部分字符串的方法。

例如,如果我有 10 条记录都包含“字符串”作为字段值的一部分(即“某事/字符串”、“某事/字符串外观”、“某事/字符串/等等”,有没有办法通过一个查询将每一行的'string'更改为'anothervalue',以便结果是'something/anothervalue'、'something/anothervaluelookhere'、'something/string/etcetera',有没有办法改变'anothervalue'

【问题讨论】:

    标签: mysql sql string sql-update


    【解决方案1】:
    UPDATE `table` SET `field` = REPLACE(`field`, 'string', 'anothervalue')
    

    【讨论】:

      【解决方案2】:

      使用LIKE 运算符查找您关心的行并使用REPLACE 函数更新它们。

      例如:

      UPDATE table_name SET field_name = REPLACE(field_name,'search','replace') WHERE field_name LIKE '%some_value%'
      

      【讨论】:

        【解决方案3】:

        我认为这应该可行:

        UPDATE table
        SET field = REPLACE(field, 'string', 'anothervalue')
        WHERE field LIKE '%string%';
        

        【讨论】:

          【解决方案4】:

          这样的事情有什么用吗?

          update table_name
          set column_name = replace(column_name, 'string%', 'string') 
          where column_name like '%string%'
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-07-31
            • 2013-12-15
            • 1970-01-01
            • 2017-07-02
            • 2022-01-01
            • 2013-06-26
            • 1970-01-01
            相关资源
            最近更新 更多