如何替换 MySQL 中包含反斜杠的字符串
How to replace strings in MySQL that contains backslashes
我尝试将 MySQL table 中的字符串替换为:
href=\"example.com\"
到
href=\"https://example.com\"
我知道如何搜索和更新字符串的正确查询,它工作得很好,直到我尝试用反斜杠更改字符串。什么都不行:
UPDATE `articles` SET `text` = REPLACE(`text`, 'href=\\"example.com\\"', 'href=\\"https://example.com\\"') WHERE `text` LIKE '%href=\\"example.com\\"%'
当然,我尝试了不同的变体和组合,有转义和没有转义的 backslaches,有 WHERE 子句也有没有。这些所有变体都不起作用。什么都没有改变!
另外,当我在 PHPMyAdmin 中预先 运行 这个查询时(第 "Find and Replace" 部分),它正确地找到了所有包含 href=\"example.com\"[ 的文章=29=],但替换字符串显示的内容与原始字符串相同。
我的 CMS 也有搜索和替换字符串的内置功能,也不能用反斜杠更改字符串。
我完全被这个问题困住了。
这是您要更新的查询;
UPDATE `articles`
SET `text` = REPLACE(`text`, 'href=\"example.com\"', 'href=\"https://example.com\"')
WHERE `text` LIKE '%href=\\"example.com\\"%'
您在替换搜索字符串上放置了太多反斜杠,因此找不到该字符串。双反斜杠在 table 中存储为一个反斜杠,因此在替换字符串时需要使用 2x 反斜杠,在 where 子句中进行搜索时需要使用 4x 反斜杠。
看这里的演示; http://sqlfiddle.com/#!9/c1e0eb/1
update articles
set text =
REPLACE(`text`, 'href=\"example.com\"', 'href=\"https://example.com\"')
where text like '%href=\\"example.com\\"%'
我尝试将 MySQL table 中的字符串替换为:
href=\"example.com\"
到
href=\"https://example.com\"
我知道如何搜索和更新字符串的正确查询,它工作得很好,直到我尝试用反斜杠更改字符串。什么都不行:
UPDATE `articles` SET `text` = REPLACE(`text`, 'href=\\"example.com\\"', 'href=\\"https://example.com\\"') WHERE `text` LIKE '%href=\\"example.com\\"%'
当然,我尝试了不同的变体和组合,有转义和没有转义的 backslaches,有 WHERE 子句也有没有。这些所有变体都不起作用。什么都没有改变!
另外,当我在 PHPMyAdmin 中预先 运行 这个查询时(第 "Find and Replace" 部分),它正确地找到了所有包含 href=\"example.com\"[ 的文章=29=],但替换字符串显示的内容与原始字符串相同。
我的 CMS 也有搜索和替换字符串的内置功能,也不能用反斜杠更改字符串。
我完全被这个问题困住了。
这是您要更新的查询;
UPDATE `articles`
SET `text` = REPLACE(`text`, 'href=\"example.com\"', 'href=\"https://example.com\"')
WHERE `text` LIKE '%href=\\"example.com\\"%'
您在替换搜索字符串上放置了太多反斜杠,因此找不到该字符串。双反斜杠在 table 中存储为一个反斜杠,因此在替换字符串时需要使用 2x 反斜杠,在 where 子句中进行搜索时需要使用 4x 反斜杠。 看这里的演示; http://sqlfiddle.com/#!9/c1e0eb/1
update articles
set text =
REPLACE(`text`, 'href=\"example.com\"', 'href=\"https://example.com\"')
where text like '%href=\\"example.com\\"%'