简体   繁体   中英

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/". Is there some kind of command such as str_replace($search, $replace, $subject) equivalent in SQL?

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?

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 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]');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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