我如何只替换 SQL 函数中的第一个结果 [英] How i can Replace Only first Result in SQL Function

查看:52
本文介绍了我如何只替换 SQL 函数中的第一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会询问SQL替换功能.

我需要你的帮助.在 Wordpress DB 中,我必须更改一小部分.我只想替换列中的第一个结果.我举个例子来解释一下,

我的字符串是 =你好 XXX 明天我们 XXX 将在 XXX 迪拜见面,你想加入我们吗XXX ?"

我的结果一定是这样=你好MMM明天我们XXX将在XXX迪拜见面,你想加入我们吗XXX ?"

在这个例子中,我们把 MMM 放在 XXX 的第一个结果上.我该怎么做?

我试过下面这段代码,但它把每一个 XXX 都替换成了 MMM;

UPDATE wp_comments SET comment_content = REPLACE (comment_content, 'XXX', 'MMM') WHERE `comment_ID`=334 LIMIT 1

解决方案

在 MySQL

8.0,不支持regexp_replace()之类的,可以使用字符串函数:

更新 wp_comments设置 comment_content = concat(substr(comment_content, 1, locate('XXX', comment_content) - 1),'嗯',substr(comment_content, locate('XXX', comment_content) + 3))其中 comment_id = 334 和 locate('XXX', comment_content) >0

DB Fiddle 演示:

<前>comment_id |评论内容---------: |:------------------------------------------------------------------------------第334话你好 MMM 明天我们 XXX 将在 XXX 迪拜见面,你想加入我们 XXX 吗?

I will ask about SQL replacement function.

I need to your bits of help. In Wordpress DB, I must change a little part. I want to replace first result only in a column. I will explain by giving an example,

My string is = "Hello XXX tomorrow we XXX will meeting in XXX Dubai, Do you want join us XXX ? "

My result must be like this= "Hello MMM tomorrow we XXX will meeting in XXX Dubai, Do you want join us XXX ? "

in this example, we putted MMM to first result of XXX . How i can do it ?

I tried below this code but, it replaced every XXX to MMM;

UPDATE wp_comments SET comment_content = REPLACE ( comment_content, 'XXX', 'MMM' ) WHERE `comment_ID`=334 LIMIT 1

解决方案

In MySQL < 8.0, where regexp_replace() and the-like are not supported, you can use string functions:

update wp_comments
set comment_content = concat(
    substr(comment_content, 1, locate('XXX', comment_content) - 1),
    'MMM',
    substr(comment_content, locate('XXX', comment_content) + 3)
) 
where comment_id = 334 and locate('XXX', comment_content) > 0

Demo on DB Fiddle:

comment_id | comment_content                                                               
---------: | :-----------------------------------------------------------------------------
       334 | Hello MMM tomorrow we XXX will meeting in XXX Dubai, Do you want join us XXX ?

这篇关于我如何只替换 SQL 函数中的第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆