【问题标题】:replace string inside the table sql替换表sql中的字符串
【发布时间】:2013-03-20 21:39:39
【问题描述】:

我遇到了一些关于用一些要求替换 mysql 表中的字符串的问题。你可以看到我当前的表:

+----+------------------------+  
| id | data                   |  
+----+------------------------+  
|  1 | [text1]mytext1[/text1] |
|    | [text2]mytext2[/text2] |
|    | [text3]mytext3[/text3] |
|    | [text4]mytext4[/text4] |
+----+------------------------+

那我需要把[text3]mytext3[/text3]改成[text3]mytext5[/text3]

问题是:

  1. 如何找到字符串 [textX] 和 [/textX] ?
  2. 如何替换 [textX] 和 [/textX] 之间的内容?

有什么想法吗?

【问题讨论】:

    标签: mysql concat


    【解决方案1】:

    使用 LIKE 查找匹配项,然后使用 REPLACE 进行更改:

    update mytable set
    data = replace(data, 'mytext3', 'mytext5')
    where data like '[text%]mytext3[/text%]'
    

    【讨论】:

    • 是的,这行得通...但是,如果我想将 [text3]...[/text3] 作为“LIKE”的要求(而不是“mytext3”作为更改的对象)你对此有何建议?
    • 我不明白。你说它有效。它还有什么?
    猜你喜欢
    • 2011-04-03
    • 2012-02-18
    • 2012-04-26
    • 2021-05-22
    • 2013-07-18
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多