MySQL 随机密码生成代码

原创
2016-06-07 18:01:38580浏览

晚上有朋友问起,简单的写了一个。

代码如下:
DELIMITER $$

CREATE
FUNCTION `t_girl` . `func_rand_string` ( f_num tinyint unsigned , f_type tinyint unsigned )
RETURNS varchar ( 32)
BEGIN
-- Translate the number to letter.
-- No 1 stands for string only.
-- No 2 stands for number only.
-- No 3 stands for combination of the above.
declare i int unsigned default 0;
declare v_result varchar ( 255) default '' ;
while i < f_num do
if f_type = 1 then
set v_result = concat ( v_result, char ( 97+ ceil( rand ( ) * 25) ) ) ;
elseif f_type= 2 then
set v_result = concat ( v_result, char ( 48+ ceil( rand ( ) * 9) ) ) ;
elseif f_type= 3 then
set v_result = concat ( v_result, substring ( replace ( uuid ( ) , '-' , '' ) , i+ 1, 1) ) ;
end if;
set i = i + 1;
end while;
return v_result;

END $ $

DELIMITER ;

调用方法示例:
代码如下:
select func_rand_string(12,3);

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
PHP中文网
程序员·梦开始的地方
下载APP