mysql中實現隨機產生字符串的方法
1.生成指定長度的隨機字符串
CREATE DEFINER=`root`@`localhost` FUNCTION `RAND_STR`(n INT) RETURNS varchar(255) CHARSET utf8 COLLATE utf8_binBEGIN
DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
DECLARE return_str varchar(255) DEFAULT '';
DECLARE i INT DEFAULT 0;
WHILE i < n DO
SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62),1));
SET i = i +1;
END WHILE;
RETURN return_str;
END
2.隨機生成字母大小寫+數字的n位字符串
drop function if exists rand_string;delimiter $$
create function rand_string(n int) returns varchar(255) CHARSET utf8
begin
declare chars_str varchar(62) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
declare return_str varchar(255) default '';
declare i int default 0;
while i
set return_str=concat(return_str,substring(chars_str,floor(1+rand()*62),1));
set i=i+1;
end while;
return return_str;
end $$