mysql使用concat更新REPLACE

时间:2016-06-07 10:53:27

标签: php mysql replace

我的表保存了相同的ID:123,9901,888,99

我需要在此字段中更改一些ID

我使用我的sql:让9901更改为2001

UPDATE `TALBE_` SET id_group = REPLACE (id_group, '9901', '2001');

该作品

但是,我将99更改为100

UPDATE `TALBE_` SET id_group = REPLACE (id_group, '99', '100');

我的sql fidle已改为

123的 10001 下,888,的 100

如何改变99 - > 100,不要改变9901?

我需要使用concat吗?

但我测试一段时间仍然无法做到

2 个答案:

答案 0 :(得分:3)

您可以使用例如

UPDATE `TALBE_` SET id_group = 
    trim(',' from REPLACE(concat(',', id_group, ','), ',99,', ',100,')); 

答案 1 :(得分:1)

她是一个寻求数字替换它的版本

您必须插入搜索值2次

SELECT CONCAT_WS (',',
      SUBSTRING_INDEX(id_group, ',', FIND_IN_SET('888',id_group)-1)
      ,'100',
      SUBSTRING_INDEX(id_group, ',', --1* (LENGTH(REGEXP_REPLACE(id_group,'[0-9]','')) -(FIND_IN_SET('888',id_group)-1)))
      );

<强>示例

MariaDB [(none)]> SELECT CONCAT_WS (',',
    ->       SUBSTRING_INDEX('123,9901,888,99', ',', FIND_IN_SET('888','123,9901,888,99')-1)
    ->       ,'100',
    ->       SUBSTRING_INDEX('123,9901,888,99', ',', --1* (LENGTH(REGEXP_REPLACE('123,9901,888,99','[0-9]','')) -(FIND_IN_SET('888','123,9901,888,99')-1)))
    ->       );
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CONCAT_WS (',',
      SUBSTRING_INDEX('123,9901,888,99', ',', FIND_IN_SET('888','123,9901,888,99')-1)
      ,'100',
      SUBSTRING_INDEX('123,9901,888,99', ',', --1* (LENGTH(REGEXP_REPLACE('123,9901,888,99','[0-9]','')) -(FIND_IN_SET('888','123,9901,888,9 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 123,9901,100,123                                                                                                                                                                                                                                                 |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>