简体   繁体   English

在MySQL中使用replace和concat

[英]Using replace and concat in MySQL

I have strings values such as the following in a column: 我在一列中有类似以下的字符串值:

|333|,|331|

I want to perform a balanced string replacement as follows: 我要执行平衡的字符串替换,如下所示:

xxTM_333_TMxx,xxTM_331_TMxx

I have tried to do this with the REPLACE and CONCAT functions but didn't get the desired output. 我尝试使用REPLACECONCAT函数来执行此操作,但未获得所需的输出。

For example: 例如:

SELECT REPLACE('|333|,|331|','|','xxTM');

This replaces one of the | 这将替换| symbols correctly in each case, but not its matched (balanced) counterpart. 符号在每种情况下均正确无误,但不匹配(平衡)的对应符号。

How can I achieve this result in MySQL? 如何在MySQL中获得此结果?

SET @st := '|333|,|331|';

SELECT
  CASE WHEN @st LIKE '|%|' THEN
    CONCAT(
      'xxTM',
      REPLACE(REPLACE(@st, '|,|', '_TMxx,xxTM_'), '|', '_'),
      'TMxx')
  END rep_st;

Please see fiddle here . 请看这里的小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM