MySQL 在数据库中搜索并将“http”替换为“https”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42857921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:15:47  来源:igfitidea点击:

Search & replace 'http' to 'https' in database

mysqlsqlphpmyadmin

提问by Henrik Petterson

Using phpmyadmin, I want to run a query that will search my entiredatabase for:

使用phpmyadmin,我想运行一个查询来搜索我的整个数据库:

http://example.com

And replace with:

并替换为:

https://example.com

My SQL knowledge is limited, maybe something like:

我的 SQL 知识有限,可能类似于:

UPDATE ?? = REPLACE(??, 'http://example.com', 'https://example.com');

The database is over 1gb, so what can I run that will notcrash the server.

数据库超过 1GB,所以我可以运行什么不会使服务器崩溃。

Update: Note that while there are other answers posted here on SO that deals with search and replace, they don't seem to cover the entire database.

更新:请注意,虽然在 SO 上发布了其他有关搜索和替换的答案,但它们似乎并未涵盖整个数据库。

回答by Bernd Buffen

use REPLACE. and if there is a index on the field then the UPDATE can use them

使用替换。如果字段上有索引,则 UPDATE 可以使用它们

UPDATE t
     set url = REPLACE(url, 'http:', 'https:')
     WHERE url LIKE '%http:%';

only change example.com

只更改example.com

this will only find row with 'http://example.com'

这只会找到带有“ http://example.com”的行

UPDATE t
     set url = REPLACE(url, 'http:', 'https:')
     WHERE url LIKE '%http://example.com%';

or this will find all rows with http:// but only change only this http://example.comto https://example.com

或者这会找到所有带有 http:// 的行,但只将此http://example.com更改为https://example.com

UPDATE t
     set url = REPLACE(url, 'http://example.com', 'https://example.com')
     WHERE url LIKE '%http:%';

回答by Gordon Linoff

I would use insert:

我会用insert

update t
     set url = insert(url, 5, 0, 's')
     where url like 'http:%';