如何替换 MySQL 中的 HTML?
我想在我的 MySQL 数据库字段之一中查找并替换(或更准确地说,附加)一些 HTML,其中该字段包含特定的文本字符串。
我可以使用此查询成功找到相关记录:
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>')
但返回了一个错误:
#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
我认为它不喜欢我格式化 HTML 的方式,但应该如何格式化它?
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.
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.
So I try the following query to append the HTML I want to use:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在尝试执行两个不同的查询。我的猜测是您需要执行以下操作:
you are trying to execute two different queries. My guess is that you need to execute the following one:
查看 UPDATE Syntax ,需要像
look on UPDATE Syntax , need to be like
您是否尝试过使用 select 作为内部查询? (假设
Have you tried using the select as an inner query? (assuming
您尝试替换的内容没有任何问题。但是,在我看来,您构建更新查询的方式是错误的:您将
SELECT
放在UPDATE
查询之前,可能期望UPDATE 只会触及
SELECT
行。我想你想要的是这样的:
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 anUPDATE
query, probably expecting theUPDATE
would only touch theSELECT
ed rows.I think what you want is this:
MySQL >= 5.5 提供了 XML 函数来解决您的问题。
您可以组合
ExtractValue
、UpdateXML
和REPLACE
函数。参考: https://dev.mysql.com/doc/ refman/5.5/en/xml-functions.html
MySQL >= 5.5 provides XML functions to solve your issue.
You can combine
ExtractValue
,UpdateXML
andREPLACE
functions.Reference: https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html