[英]How can I replace HTML in MySQL?
I want to find and replace (or more accurately, append) some HTML in one of my MySQL database fields, where the field contains a specific string of text. 我想在我的一个MySQL数据库字段中找到并替换(或更准确地说,附加)一些HTML,其中该字段包含特定的文本字符串。
I can successfully find the relevant records using this query: 我可以使用此查询成功找到相关记录:
SELECT *
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%'
This query returns all the records I want to append HTML to. 此查询返回我要将HTML追加到的所有记录。
So I try the following query to append the HTML I want to use: 所以我尝试以下查询来附加我想要使用的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>')
But an error is returned: 但是返回错误:
#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 'UPDATE wp_posts SET post_content = INSERT(post_content, '</strong></a>', '</stro' at line 4
I presume it doesn't like how I've formatted my HTML, but how should it be formatted? 我认为它不喜欢我如何格式化我的HTML,但它应该如何格式化?
look on UPDATE Syntax , need to be like 看看UPDATE语法 ,需要像
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%'
you are trying to execute two different queries. 您正在尝试执行两个不同的查询。 My guess is that you need to execute the following one:
我的猜测是你需要执行以下一个:
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 provides XML functions to solve your issue. MySQL> = 5.5提供XML函数来解决您的问题。 You can combine
ExtractValue
, UpdateXML
and REPLACE
functions. 您可以组合
ExtractValue
, UpdateXML
和REPLACE
函数。
Reference: https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html 参考: https : //dev.mysql.com/doc/refman/5.5/en/xml-functions.html
Have you tried using the select as an inner query? 您是否尝试将select用作内部查询? (assuming
(假设
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%')
There's nothing wrong with the content you're trying to replace. 您尝试替换的内容没有任何问题。 However, the way I see it, you construct your update query the wrong way: You put a
SELECT
right before an UPDATE
query, probably expecting the UPDATE
would only touch the SELECT
ed rows. 但是,按照我的方式,您以错误的方式构造更新查询:在
UPDATE
查询之前放置SELECT
,可能期望UPDATE
只触及SELECT
ed行。
I think what you want is this: 我想你想要的是这个:
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%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.