mysql 随机 生成_MySQL随机字符串生成

drop function if exists rand_string;

create function rand_string(str_length tinyint unsigned, str_type tinyint unsigned) returns varchar(255)

begin

-- Function : rand_string

-- Author : reymondtu#opencfg.com

-- Date : 2011/03/27

-- Params : str_length int unsigned

-- The random string length of random string

-- str_type int unsigned

-- The random string type

-- 1.0-9

-- 2.a-z

-- 3.A-Z

-- 4.a-zA-Z

-- 5.0-9a-zA-Z

--

-- Example :

--

-- mysql> select rand_string(32,5) from dual;

-- +----------------------------------+

-- | rand_string(32,5) |

-- +----------------------------------+

-- | HbPBz4DWSAiJNLt4SgExHVwQI34bI6mt |

-- +----------------------------------+

-- 1 row in set

declare counter int unsigned default 0;

declare const_chars varchar(64) default '0123456789';

declare result varchar(255) default '';

if str_type = 1 then

set const_chars = '0123456789';

elseif str_type = 2 then

set const_chars = 'abcdefghijklmnopqrstuvwxyz';

elseif str_type = 3 then

set const_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

elseif str_type = 4 then

set const_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

elseif str_type = 5 then

set const_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

else

set const_chars = '0123456789';

end if;

while counter < str_length do

set result = concat(result,substr(const_chars,ceil(rand()*(length(const_chars)-1)),1));

set counter = counter + 1;

end while;

return result;

end

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-03-28 00:15

浏览 6671

论坛回复 / 浏览 (0 / 2470)

分类:数据库

评论

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值