-- MySQL随机生成登录验证码函数
set global log_bin_trust_function_creators=on;
-- 解决1418错误 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
delimiter $$
create function randVerificationCode(n int) returns varchar(255)
begin
-- 声明一个包含字母和数字的字符串str,共62个字符,
-- 其中包括26个字母大小写和0-9共10个数字。
declare str varchar(255) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
-- 声明一个变量i,记录当前字符的位置
declare i int default 0;
-- 声明返回字符串resStr
declare resStr varchar(255) default '';
while i < n do
set resStr=CONCAT(resStr,substr(str,floor(rand()*62 1),1));
set i=i 1;
end while;
return resStr;
end$$
delimiter ;
set global log_bin_trust_function_creators=off;
评论