MySQL:为什么 url 在使用 REPLACE 时不匹配?

MySQL: Why aren't url's matching when using REPLACE?

我的情况:

我在包含博客 post 的字段中有 url。 url 正在使用转义字符存储在我的数据库中。我现在的任务是用[=​​66=] url替换一些已经插入的'http' url,但是REPLACE既不匹配原来的url也不匹配逃脱的url。我不能只替换 'http:' 的每个实例,因为我只想影响每个 post 中的某些 link,而不是每个 link.

我对SQL和REPLACE非常熟悉,所以我不只是问REPLACE如何工作以及如何使用它。这里的另一个用户已经在他的环境中测试了我的查询并且它们有效。因此,我的配置中一定有什么东西阻止了查询按预期运行。

我已经在这个站点和 google 上广泛搜索了几个小时,但没有找到任何专门解决我的问题的内容。我尝试过的所有内容都包含在下面,如果还有其他我应该尝试的东西,我不知道那是什么,我还没有发现任何 suggestions/posts/comments 建议做任何不同的事情。

示例URL:

http://test01.mysite.com

存储在数据库中:

http:\/\/test01.mysite.com

重建情境的代码:

DROP TABLE IF EXISTS test_posts;
CREATE TABLE IF NOT EXISTS test_posts (
    id int NOT NULL AUTO_INCREMENT,
    post_content longtext NOT NULL,
    PRIMARY KEY (id)
)

INSERT INTO 
    test_posts
        (post_content)
    VALUES 
        ('content content content <a href="http:\/\/test01.mysite.com">Link I want to change</a> content content content <a href="http:\/\/someothersite.com">Link I don\'t want to change</a> content content content <a href="http:\/\/test01.mysite.com">Link I want to change</a> content content content <a href="http:\/\/someothersite.com">Link I don\'t want to change</a>');

如果我运行

UPDATE
    test_posts
SET
    post_content = REPLACE(post_content, 'http://test01.mysite.com', 'https://test01.mysite.com');

UPDATE
    test_posts
SET
    post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');

零条记录受到影响。

出于测试目的,我 运行 以下查询 return 有 0 行。

SELECT
    *
FROM
    test_posts
WHERE
    post_content LIKE '%http://test01.mysite.com%'
    OR
    post_content LIKE '%http:\/\/test01.mysite.com%'
    OR
    post_content LIKE '%http:\/\/test01.mysite.com%'
    OR
    post_content LIKE 'http:%/%/test01.mysite.com%';

如果我运行:

SELECT 
    *
FROM
    test_posts 
WHERE 
    post_content LIKE '%http:_/_/test01.mysite.com%'

它确实return匹配,但这并没有解决使用UPDATE/REPLACE时如何匹配的实际问题。

我在两台不同的服务器上试过,我在两台服务器上都得到了相同的结果。

我已经尝试了以下 Engine/Collation 组合并且所有 return 相同的 0 条记录结果:

MyISAM/latin1_swedish_ci

MyISAM/utf8mb4_unicode_ci

InnoDB/latin1_swedish_ci

InnoDB/utf8mb4_unicode_ci

有人知道我如何编写这些查询以便 REPLACE 找到与那些 url 的匹配项,或者我的数据库或 PhpMyAdmin 中的哪些设置可能导致查询 return/affect 0 行?

没有理由,如果你 运行 正确,你的查询将无法工作,还有别的东西,你可能在这里遗漏了。

UPDATE test1 SET name_1 = REPLACE(name_1, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com')

运行良好,并完成了用 / 替换 \/ 的工作。 请参阅随附的屏幕截图,

您可能还有其他问题,如果是,请检查并更新问题。

评论后编辑

如果您在 URL 中有更多数据点,请像下面这样更改查询。

UPDATE test1 SET name_1 = REPLACE(name_1, '\/', '/')

以上将用 / 替换所有出现的 \/

我认为 MySQL

中的反斜杠必须转义
field_name LIKE 'http:\/\/test01.mysite.com%'

当然可以使用单字符通配符 __

field_name LIKE 'http:_/_/test01.mysite.com%'

或者对于你的两种情况:一个可选的反斜杠:

field_name LIKE 'http:%/%/test01.mysite.com%'

我仍然对为什么使用 LIKE 的查询不起作用感到困惑,但遗憾的是,使用这些来缩小问题范围使我的判断蒙上了阴影,我没有在 REPLACE 函数中尝试所有相同的组合.

以下作品:

UPDATE
    test_posts
SET 
    post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');

如果有人能向我解释为什么这些组合适用于 REPLACE 而不适用于 LIKE,我真的很想知道。谢谢!

由于 \ 对 represent/escape 反斜杠不起作用,请使用 regular expression 函数:

REGEXP_LIKE('.*http:\/\/test01\.mysite.com.*')
REGEXP_REPLACE(field, 'http:\/\/', 'http://')

此处 \ 应该有效。