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

时间:2018-06-15 18:07:08

标签: mysql select replace

我想在mysql select query中只用一个替换语句替换很多关键字。

我的桌子就像

enter image description here

I/flutter (21751): The following assertion was thrown during performResize():
I/flutter (21751): Vertical viewport was given unbounded height.
I/flutter (21751): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter (21751): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter (21751): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter (21751): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter (21751): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter (21751): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter (21751): the height of the viewport to the sum of the heights of its children.

我想用concat('_',Keywords,'__')替换匹配的关键字。

需要帮助。

1 个答案:

答案 0 :(得分:1)

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

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');

结果是:你好我的名称