晚上有朋友问起,简单的写了一个。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);
用MySQL 生成随机密码
最新推荐文章于 2023-08-30 23:57:05 发布
本文介绍了一种在MySQL中创建随机字符串的方法,通过定义存储过程实现不同类型的随机字符串生成,包括纯字母、纯数字及字母数字混合等。该函数利用UUID、随机数生成等特性,适用于多种应用场景。
1112

被折叠的 条评论
为什么被折叠?



