简体   繁体   English

MySQL更新整个字符串的一部分?

[英]MySQL update part of a whole string?

I store numeric variables in a string. 我将数字变量存储在字符串中。 Like:165,37,0,0,21 喜欢:165,37,0,0,21

Now i have to change only the last part but keep the rest the same. 现在,我只需要更改最后一部分,但其余部分保持不变。 (in my example i need to change 21 to 0) (在我的示例中,我需要将21更改为0)

Do you have any ideas? 你有什么想法?

我为此使用正则表达式,下面的reg ex将在字符串的末尾找到数字,包括最后一个逗号,并将其替换为',0'

UPDATE table SET column1 = REGEXP_REPLACE(column1 , ',[0-9]*$', ',0')

REPLACE would be most ideal in this case: 在这种情况下,REPLACE是最理想的:

UPDATE tbl_name 
SET 
field_name = REPLACE(field_name,
    string_to_find,  -- in your case 21
    string_to_replace  -- in your case 0 ) 
WHERE
<place condition if any e.g. the key to that record>;

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

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