用随机元音替换 mysql 列中的所有元音

replace all the vowels in a mysql column with a random vowel

我有一个带有 table 客户端的 mysql 数据库,我想通过替换 FirstName 和 LastName 列的所有元音来隐藏我的敏感数据(将数据库提供给开发人员)通过随机元音。

所以所有在字符串中找到的"A"、"E"、"I"、"O"、"U"都应该替换为字符串中的随机字母组 "A"、"E"、"I"、"O"、"U".

同样,所有 "a"、"e"、"i"、"o"、"u" 都应替换为组中的随机字母 "a", "e", "i", "o", "u".

感谢您的回答。

像这样的东西会起作用,

select translate('The quick brown fOx jumps ovEr the lazy 
dog','aeiouAEIOU','eiouaEIOUA');

                  translate                  
---------------------------------------------
 Thi qaock bruwn fUx jamps uvIr thi lezy dug

translate(column, to_string, from_string) - 对于 from_string,您可以按任意顺序指定字母。 参考文档here

但是一个字符只能被一个字符替换, 即 'a' 将替换为 'e',等等

这不是很漂亮,但我工作。

SELECT name,
    replace(
    replace(
    replace(
    replace(
    replace(
    replace(
        name,'a',mid('aeiuoy',rand()*5+1,1))
            ,'e',mid('aeiuoy',rand()*5+1,1))
            ,'i',mid('aeiuoy',rand()*5+1,1)) 
            ,'u',mid('aeiuoy',rand()*5+1,1)) 
            ,'o',mid('aeiuoy',rand()*5+1,1)) 
            ,'y',mid('aeiuoy',rand()*5+1,1)) 
        as codedname
FROM names;

如果你想更随机地替换,你应该创建一个函数:

CREATE FUNCTION `fReplaceVowels`(s varchar(255)) RETURNS varchar(255) CHARSET latin1
BEGIN
declare v_max int unsigned;
declare v_counter int unsigned default 1;
declare result varchar(255);

set result = s;
set v_max = LENGTH(s);

  while v_counter < v_max do
    if (select binary  'aeiuoy' like concat('%', mid(s,v_counter,1),'%') ) then
        set result=concat(left(result,v_counter-1),mid('aeiuoy',rand()*5+1,1),mid(result,v_counter+1));
    end if;
    if (select binary  'AEIUOY' like concat('%', mid(s,v_counter,1),'%') ) then
        set result=concat(left(result,v_counter-1),mid('AEIUOY',rand()*5+1,1),mid(result,v_counter+1));
    end if;
    set v_counter=v_counter+1;
  end while;
RETURN result;
END

这样使用:

SELECT name, fReplaceVowels(name) as codedname FROM names;