在select语句中用不同的位置替换多个关键字

nkcskrwz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(329)

我想在mysql select查询中用一个replace语句替换许多关键字。
我的table像

select replace(Text,' ',' ');

我想用concat(''''u1',keywords,''u2')替换匹配的关键字。
我需要帮助。

mi7gmzs6

mi7gmzs61#

你可以这样试试。这可能会有帮助。

CREATE FUNCTION `SPLIT_STR`(
  x text,
  delim VARCHAR(12),
  pos INT
) RETURNS varchar(255) CHARSET utf8
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '')

CREATE  FUNCTION `GDPR_find_keyword_and_color_them`(qtntext text,keyword nvarchar(50000)) RETURNS text CHARSET utf8
BEGIN

declare ttlkeyword bigint;
declare loopvar bigint;
declare keyword_ nvarchar(1000);
set ttlkeyword=(select count(*) from nsmx_beta4.privacy_policy_keywords);

set loopvar=1;
                while(loopvar<ttlkeyword) do
                                set keyword_=(select SPLIT_STR(keyword,',',loopvar));
                                if(keyword_!='')  then
                                                set qtntext=replace(qtntext,keyword_,concat('<b>',keyword_,'</b>'));
                                end if;
                                set loopvar=loopvar+1;
                end while;    
return qtntext;
END

select GDPR_find_keyword_and_color_them('hello my name is','my,is');

结果是:你好我的名字

相关问题