I have a WordPress table wp_posts
and I'd like to run an UPDATE that will replace a url from my old domain to my new domain.
For example, let's say that one record in the field post_content
has the following content:
This is my <a href="http://www.my-old-site.com/link/to/some/page">old web</a> site.
<img src="http://www.my-old-site.com/wp-content/upload/2012/02/my-image-file.jps />
I'd like that to become:
This is my <a href="http://www.my-new-site.com/link/to/some/page">old web</a> site.
<img src="http://www.my-new-site.com/wp-content/upload/2012/02/my-image-file.jps />
I've tried the following query
UPDATE wp_posts AS w`
SET w.post_content = REPLACE(w.post_content, 'my-old-site.com', 'my-new-site.com');
but I don't get any result.
Any idea how to fix it?
Nick Chammas
14.8k17 gold badges77 silver badges124 bronze badges
asked Mar 13, 2012 at 10:51
-
What do you mean by "don't get any result"? Is there an error message, or does the string just not get updated? I just tried your above test case on MySQL 5.1.61 & it worked fine. "Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0"Philᵀᴹ– Philᵀᴹ2012年03月13日 11:24:47 +00:00Commented Mar 13, 2012 at 11:24
-
Found this searching for "mysql longtext find and replace" and I'm also updating all old wordpress posts from an old URL to a new one. Small world.Hartley Brody– Hartley Brody2014年12月26日 21:32:52 +00:00Commented Dec 26, 2014 at 21:32
1 Answer 1
There is an error in your query near
AS w`
Alias name "w" should instead be
`w`
Try this:
UPDATE wp_posts AS `w`
SET w.post_content = REPLACE(w.post_content, 'my-old-site.com', 'my-new-site.com');
Nick Chammas
14.8k17 gold badges77 silver badges124 bronze badges
-
1You don't even need to use quotes round the alias. UPDATE wp_posts AS w SET w.post_content = REPLACE(w.post_content, 'my-old-site.com', 'my-new-site.com'); works finePhilᵀᴹ– Philᵀᴹ2012年03月13日 11:28:46 +00:00Commented Mar 13, 2012 at 11:28
-
1Yes Phil, it can also be used without rounding with quote. I written that way because one quote was missing in the query.neeraj– neeraj2012年03月13日 11:32:54 +00:00Commented Mar 13, 2012 at 11:32
lang-sql