如何替换 SQL 中的部分字符串

How to replace part of string in SQL

我在数据库列 URL "guid"

http://example.com/wp-content/uploads/2014/03/Waterproofing2.png

我需要将其更改为

http://example.com/blog/wp-content/uploads/2014/03/Waterproofing2.png

我需要将所有 URL 替换为

http://example.com/wp-content/uploads/

http://example.com/blog/wp-content/uploads/

使用replace函数:

update `table` set `column`= replace (`column`, 'http://example.com/','http://example.com/blog/') where `column`like 'http://example.com/blog/wp-content/uploads%'

嘿,最简单的方法是在 sql

中使用替换函数

简单

REPLACE(YourString, ‘text to replace’, ‘replace with text’)

REPLACE 根据输入的排序规则执行比较。要在指定的排序规则中执行比较,您可以使用 COLLATE 将显式排序规则应用于输入。

在 SQL 中,通配符与 SQL LIKE 运算符一起使用。

SQL 通配符用于搜索 table 内的数据。

和SQL,一些通配符是:

Wildcard    Description
%   A substitute for zero or more characters
_   A substitute for a single character

所以最快的方法 使用连接: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

所以一个朋友的例子,改成下面的

styles/default/xenmoods/pants.png

styles/default/xenmoods/andrew.png

styles/default/xenmoods/rawr.png

至此

http://cdn.sociallyuncensored.com/styles/default/xenmoods/pants.png
http://cdn.sociallyuncensored.com/styles/default/xenmoods/andrew.png
http://cdn.sociallyuncensored.com/styles/default/xenmoods/rawr.png

代码:

UPDATE YOURTABLE SET path =CONCAT('http://example.com/blog/wpcontent/uploads/', path) ... where ..etc

我总是使用这个查询来移动 WordPress 数据库

UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = REPLACE (guid, 'http://olddomain.com', 'http://newdomain.com');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://olddomain.com', 'http://newdomain.com');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://olddomain.com', 'src="http://newdomain.com');
UPDATE wp_posts SET guid = REPLACE (guid, 'http://olddomain.com', 'http://newdomain.com') WHERE post_type = 'attachment';
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://olddomain.com','http://newdomain.com');