MySQL存储过程和字符串操作 [英] MySQL stored procedures and string manipulation

查看:600
本文介绍了MySQL存储过程和字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要在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" ?

无论如何,您将要使用UPDATEREPLACE命令.

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 the UPDATE and REPLACE 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆