Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4195969
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2009-09-12 00:01:59

晚上有朋友问起,简单的写了一个。
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);
阅读(4498) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-09-22 11:15:02

能不能介绍一些MYSQL函数方面的学习资料,谢了!