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

时间:2015-02-06 08:56:28

标签: mysql sql string wordpress url

我在db列" guid"

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

我需要将其更改为

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

我需要用

替换所有网址
http://example.com/wp-content/uploads/

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

3 个答案:

答案 0 :(得分:3)

Heya最简单的方法是在sql中使用replace函数

简单

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

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

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

SQL通配符用于搜索表中的数据。

使用SQL,一些通配符是:

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

所以最快的方式     使用CONCAT:     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

CODE:

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

答案 1 :(得分:0)

使用replace功能:

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

答案 2 :(得分:0)

我总是将此查询用于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');