如何替换MySQL中的HTML?
我想找到并替换,或者更准确地说,在我的一个MySQL数据库字段中添加一些HTML,该字段包含一个特定的文本字符串 我可以使用此查询成功找到相关记录:如何替换MySQL中的HTML?,mysql,html,wordpress,phpmyadmin,mysql-error-1054,Mysql,Html,Wordpress,Phpmyadmin,Mysql Error 1054,我想找到并替换,或者更准确地说,在我的一个MySQL数据库字段中添加一些HTML,该字段包含一个特定的文本字符串 我可以使用此查询成功找到相关记录: SELECT * FROM `wp_posts` WHERE `post_content` LIKE '%some phrase%' 此查询返回我要附加HTML的所有记录 因此,我尝试以下查询来附加我想要使用的HTML: SELECT * FROM `wp_posts` WHERE `post_content` LIKE '%some phras
SELECT *
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%'
此查询返回我要附加HTML的所有记录
因此,我尝试以下查询来附加我想要使用的HTML:
SELECT *
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%'
UPDATE wp_posts SET post_content = REPLACE(post_content, '</strong></a>', '</strong></a>
<strong>Some additional piece of text</strong></p>')
我想它不喜欢我格式化HTML的方式,但它应该如何格式化呢
UPDATE wp_posts
SET post_content = REPLACE(post_content, '</strong></a>', '</strong></a>
<strong>Some additional piece of text</strong></p>')
WHERE `post_content` LIKE '%some phrase%'
您正在尝试执行两个不同的查询。我猜您需要执行以下操作:
UPDATE wp_posts SET post_content = REPLACE(post_content, '</strong></a>', '</strong></a>
<strong>Some additional piece of text</strong></p>')
WHERE `post_content` LIKE '%some phrase%'
您是否尝试过使用select作为内部查询?假定
UPDATE wp_posts SET post_content = REPLACE(post_content, '</strong></a>', '</strong></a>
<strong>Some additional piece of text</strong></p>')
WHERE id in (SELECT id
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%')
您尝试替换的内容没有问题。然而,在我看来,您构建更新查询的方式是错误的:您将SELECT放在更新查询之前,可能期望更新只会触及选定的行 我想你想要的是:
UPDATE wp_posts
SET post_content = REPLACE(
post_content,
'</strong></a>',
'</strong></a><strong>Some additional piece of text</strong></p>'
)
WHERE `post_content` LIKE '%some phrase%'
MySQL>=5.5提供XML函数来解决您的问题。 您可以组合ExtractValue、UpdateXML和REPLACE函数
参考资料:太棒了!谢谢-我明白我在那里想做什么了:-谢谢你们的回复,伙计们:-
UPDATE wp_posts
SET post_content = REPLACE(
post_content,
'</strong></a>',
'</strong></a><strong>Some additional piece of text</strong></p>'
)
WHERE `post_content` LIKE '%some phrase%'