简体   繁体   English

如何在MySQL中查找和替换?

[英]How can I find and replace in MySQL?

I need to change the server paths that were stored in my database (Wordpress), so I am searching for the string "/home/". 我需要更改存储在数据库(Wordpress)中的服务器路径,因此我正在搜索字符串“ / home /”。 Is there some kind of command such as str_replace($search, $replace, $subject) equivalent in SQL? 在SQL中是否存在诸如str_replace($search, $replace, $subject)等价的命令?

Edit: What if I don't know what the field name is? 编辑:如果我不知道字段名称是什么怎么办? Well, I do, but there is more than one field name. 好吧,我知道,但是有多个字段名称。 I was just hoping for a more "global" solution like in Notepad++ where I can just find all and replace all, but it seems I can only update a certain field / table? 我只是希望有一个更“全局”的解决方案,例如在Notepad ++中,我可以查找所有内容并替换所有内容,但是看来我只能更新某个字段/表?

UPDATE mytable 
   SET server_path = REPLACE(server_path,'/home/','/new_home/');

Link to documentation . 链接到文档

Edit: 编辑:
If you need to update multiple fields you can string them along—with commas in between—in that same UPDATE statement, eg: 如果您需要更新多个字段,则可以在同一条UPDATE语句中将它们一起(中间用逗号分隔),例如:

UPDATE mytable 
   SET mycol1 = REPLACE(mycol1,'/home/','/new_home/'), 
       mycol2 = REPLACE(mycol2,'/home/','/new_home/');
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM