用MySQL 产生随机密码


2009-09-12 00:03:51
 标签:MySQL 随机 密码   [推送到技术圈]

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://yueliangdao0608.blog.51cto.com/397025/201086
晚上有朋友问起,简单的写了一个。
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);

本文出自 “上帝,咱们不见不散!” 博客,请务必保留此出处http://yueliangdao0608.blog.51cto.com/397025/201086



上一篇 几个要注意的TIPS  下一篇 婚礼归来

类别:SQL语句与特殊技巧 技术圈()阅读() ┆ 评论()推送到技术圈返回首页  


    文章评论
 
 

发表评论

昵   称:
验证码:  请点击后输入验证码 博客过2级,无需填写验证码
内   容: