MySQL随机字符串生成
2013-11-26 11:13
351 查看
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
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
相关文章推荐
- MySQL中随机生成固定长度字符串的方法
- mysql随机生成字符串
- mysql存储过程,生成随机字符串
- 【转】MySQL随机字符串生成
- MySQL中随机生成固定长度字符串的方法
- 用MySQL里的Rand()生成 不连续重复 的随机数年龄以及随机姓名字符串
- Python随机生成指定长度字符串并保存到mysql中
- MySQL中随机生成固定长度字符串的方法
- mysql生成随机字符串
- 用MySQL里的Rand()生成 不连续重复 的随机数年龄以及随机姓名字符串
- C++_生成随机字符串
- [C#]生成一串包含字符和数字的随机字符串的方法
- php生成指定位数的随机字符串
- javascript随机生成字符串
- C#练习——去掉字符串两端的空格、统计字符出现的次数及出现的索引位置、随机生成10个1-100之间的不重复的偶数
- PHP生成随机字符串的方法
- Qt如何上传zip文件(两种形式),及随机生成规定长度的字符串的小方法
- 使用java Random动态传递位数 生成指定位数的随机字符串-数字字母混合
- MySQL 随机生成各种类型的随机函数
- JS生成随机的由字母数字组合的字符串