如何替换 MySQL 中的 HTML?

发布于 2024-11-27 05:16:10 字数 885 浏览 0 评论 0原文

我想在我的 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, '&lt;/strong&gt;&lt;/a&gt;​', '&lt;/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

煮茶煮酒煮时光 2024-12-04 05:16:10

您正在尝试执行两个不同的查询。我的猜测是您需要执行以下操作:

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%'
花期渐远 2024-12-04 05:16:10

查看 UPDATE Syntax ,需要像

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%'

look on UPDATE Syntax , need to be like

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%'
人│生佛魔见 2024-12-04 05:16:10

您是否尝试过使用 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%')

Have you tried using the select as an inner query? (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%')
度的依靠╰つ 2024-12-04 05:16:10

您尝试替换的内容没有任何问题。但是,在我看来,您构建更新查询的方式是错误的:您将 SELECT 放在 UPDATE 查询之前,可能期望 UPDATE 只会触及 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%'

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 SELECTed rows.

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%'
绮筵 2024-12-04 05:16:10

MySQL >= 5.5 提供了 XML 函数来解决您的问题。
您可以组合 ExtractValueUpdateXMLREPLACE 函数。

参考: 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 and REPLACE functions.

Reference: https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文