Mysql - 将字符串的一部分复制到另一个字段并删除该字符串

Mysql - copy part of a string to another field and delete the string

在 Mysql 我的一个字段中我有这个字符串 -

some text [= ajunkt]

我想复制方括号内的部分字符串,正好

ajunkt

到另一个字段并删除[= ajunkt]

如果模式相同,您可以使用 substring_index 将字符串的那部分作为

mysql> select substring_index(substring_index('some text [= ajunkt]','[=',-1),']',1) as copy;
+---------+
| copy    |
+---------+
|  ajunkt |
+---------+
1 row in set (0.00 sec)

现在要将文本复制到另一个文件中,您可以使用 as

update table_name
set 
copy_col_name = substring_index(substring_index(col_name','[=',-1),']',1);

最后,您可以将现有字符串中的那部分替换为

 select 
 replace(
   'some text [= ajunkt]',
   concat(
     '[= ',
     trim(
       substring_index(
         substring_index('some text [= ajunkt]',
         '[=',-1),
        ']',1
        ) 
      ),
    ']'
  ),
'') as new_str ;


+------------+
| new_str    |
+------------+
| some text  |
+------------+

将上面的 select 更改为更新并将硬编码输入值替换为列名。