MySQL存储过程和字符串操作 [英] MySQL stored procedures and string manipulation
问题描述
你好,
我需要在MySQL中编写一个存储过程,该存储过程循环遍历表中的所有行(列类型为TEXT和VARCHAR),并将列值中的某些字符替换为另一个字符.例如,某列包含文本"ababa",而我想将其更改为"acaca".
我不太清楚将字符串中的字符替换为另一个字符的部分,对此可能会有所帮助!
非常感谢!
Sylvain
Hello,
I need to write a stored procedure in MySQL that loops through all rows in a table (the column types are TEXT and VARCHAR) and replaces certain characters in the column values with another character. For example, a certain column contains the text "ababa" and I want to change it to "acaca".
I can''t quite figure out the part about replacing a character in a string with another and could use some help with this!
Thank you very much!
Sylvain
推荐答案
如果要替换字符或字符串,则需要进行排序.字母"b"与单词"ababa"不同,因此您应该更加清楚:要将"b"替换为"c"还是"abab"替换为"acaca" ?
无论如何,您将要使用UPDATE
和REPLACE
命令.
You need to sort out if you''re wanting to replace a char or a string. The letter ''b'' is different than the word "ababa", so you should be a little more clear there: are you wanting to replace ''b'' with ''c'' or "ababa" with "acaca"?
Regardless, you''re going to want to use theUPDATE
andREPLACE
commands.
UPDATE your_table SET your_field=(REPLACE(your_field,''old-text'',''new-text'')
old-text
是您要替换的字符,new-text
是您要替换的字符.
干杯,
-jc
old-text
is (are) the character(s) you wish to replace, and new-text
is (are) the character(s) you''ll replace them with.
Cheers,
-jc
这篇关于MySQL存储过程和字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!