1

I need to replace an html code in multiple posts. This is the code I am using:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'target="_top" class="text"') WHERE 'post_content'  = 'class="text"'

But I am getting an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') WHERE 'post_content' = 'class="text"'' at line 1

Is there any solution to make it work properly ?

4

3 回答 3

3

REPLACE takes three arguments, you only gave two. You didn't tell it what to replace the matched text with.

于 2012-08-31T19:06:17.530 回答
1

请查看手册以正确使用REPLACE. 您需要提供 3 个参数:

REPLACE(str, from_str, to_str)

返回字符串 str,其中所有出现的字符串 from_str 都替换为字符串 to_str。REPLACE() 在搜索 from_str 时执行区分大小写的匹配。

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
        -> 'WwWwWw.mysql.com'
于 2012-08-31T19:08:38.963 回答
1

你需要三个参数来替换,你还需要LIKE在你的WHERE子句中使用,否则你不会得到任何匹配。最后,您不希望post_contentWHERE子句中使用单引号。要么使用反引号,要么什么都不用。

UPDATE wp_posts
SET post_content = REPLACE(post_content, 'class="text"', 'target="_top" class="text"')
WHERE post_content LIKE '%class="text"%'
于 2012-08-31T19:09:56.500 回答