通常我使用手动查找替换文本在MySQL数据库使用phpMyAdmin。我现在厌倦了,我怎么能运行一个查询,在phpMyAdmin的整个表中找到和替换一个新的文本?
例如:查找关键字domain。例如,替换为www.domain.example。
通常我使用手动查找替换文本在MySQL数据库使用phpMyAdmin。我现在厌倦了,我怎么能运行一个查询,在phpMyAdmin的整个表中找到和替换一个新的文本?
例如:查找关键字domain。例如,替换为www.domain.example。
当前回答
生成变更SQL查询(FAST)
mysql -e "SELECT CONCAT('update ', table_name,' set ', column_name,' = replace(', column_name,', " www.oldsite.example ", " www.newsite.example ");') AS语句FROM information_schema。-u root -p your_db_name_here > upgrade_script.sql . WHERE table_name LIKE 'wp_%'
删除文件开头的任何垃圾。我吃了一些。
纳米upgrade_script.sql
使用——force选项运行生成的脚本以跳过错误。(慢速-如果DB大,可以喝杯咖啡)
Mysql -u root -p your_db_name_here——force < upgrade_script.sql
其他回答
把它放到一个php文件中,然后运行它,它应该做你想做的事情。
// Connect to your MySQL database.
$hostname = "localhost";
$username = "db_username";
$password = "db_password";
$database = "db_name";
mysql_connect($hostname, $username, $password);
// The find and replace strings.
$find = "find_this_text";
$replace = "replace_with_this_text";
$loop = mysql_query("
SELECT
concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''{$find}'', ''{$replace}'');') AS s
FROM
information_schema.columns
WHERE
table_schema = '{$database}'")
or die ('Cant loop through dbfields: ' . mysql_error());
while ($query = mysql_fetch_assoc($loop))
{
mysql_query($query['s']);
}
在phpMyAdmin中运行SQL查询来查找和替换所有WordPress博客文章中的文本,例如查找mysite。Example /wordpress,然后用mysite。Example /news替换 本例中的表为tj_posts
UPDATE `tj_posts`
SET `post_content` = replace(post_content, 'mysite.example/wordpress', 'mysite.example/news')
UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);
比如,如果我想把所有出现的约翰替换为马克,我会用下面的方法,
UPDATE student SET student_name = replace(student_name, 'John', 'Mark');
我相信“swapnesh”答案是最好的!不幸的是,我不能执行它在phpMyAdmin(4.5.0.2)谁虽然不合逻辑(并尝试了几件事),它一直说,一个新的语句被发现,没有分隔符被发现…
因此,我提出了以下解决方案,如果您遇到同样的问题,并且除了PMA之外没有其他访问数据库的方法,可能会很有用……
UPDATE `wp_posts` AS `toUpdate`,
(SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
SET `toUpdate`.`guid`=`updated`.`guid`
WHERE `toUpdate`.`ID`=`updated`.`ID`;
要测试预期的结果,您可能需要使用:
SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
FROM `wp_posts` AS `toUpdate`,
(SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
WHERE `toUpdate`.`ID`=`updated`.`ID`;
在有大小写字母的句子中, 我们可以使用二进制REPACE
UPDATE `table_1` SET `field_1` = BINARY REPLACE(`field_1`, 'find_string', 'replace_string')