Mysql使用函数生成指定随机字段内容

Mysql使用函数生成指定随机字段内容。

函数rand_name生成随机字符串:

 1 drop function if exists rand_name;
 2 delimiter //
 3 -- 生成用户姓名
 4 create definer=`root`@`192.168.1.244` function `rand_name`(n int) returns varchar(255) charset utf8
 5 begin
 6     declare char_str varchar(255) default '哈哈章与张霞笑小欧由于语塔堆太湖想笑一二三四五六七八九十宇宇你是最耀眼的星星再干一杯永远生命不过短暂的烟火爱你所爱无问西东';
 7     declare return_name varchar(255) default '';
 8     declare i int default 0;
 9     while i < n do
10         set return_name = concat(return_name, substring(char_str, floor(1+rand()*30),1));
11         set i = i + 1;
12     end while;
13     return return_name;
14 end//
  delimiter ;

 

函数get_phone生成电话号码,函数phone_head用于生成电话号码头部,利用函数rand_string生成号码后8位:

 

 1 -- 生成电话号码
 2 drop function if exists `phone_head`;
 3 delimiter //
 4 create definer=`root`@`192.168.1.243`  function `phone_head`() returns char(3) charset utf8
 5 begin
 6     declare head char(3);
 7     declare bodys varchar(225) default'130 131 132 133 134 135 136 137 138 139 186 187 189 151 157';
 8     declare starts int;
 9     set starts = 1 + floor(rand()*15)*4 ;
10     set head =trim(substring(bodys,starts,3));
11     return head;
12 end//
13 delimiter ;
14 
15 -- 生成随机数字串
16 drop function if exists `rand_string`;
17 delimiter //
18 create definer=`root`@`192.168.1.243` function `rand_string`(n int) returns varchar(255) charset utf8
19 begin
20     declare char_str varchar(255) default '0123456789';
21     declare return_str varchar(255) default '';
22     declare i int default 0; 
23     while i < n do
24         set return_str=concat(return_str,substring(char_str,floor(1+rand()*10),1));
25         set i=i+1;
26     end while;
27     return return_str;
28 end //
29 delimiter ;
30 
31 drop function if exists `get_phone`;
32 delimiter //
33 create definer=`root`@`192.168.1.243` function `get_phone`() returns varchar(20) charset utf8
34 begin
35     declare phone varchar(20);
36     set phone = trim(concat(phone_head(),rand_string(8)));
37     return phone;
38 end //
39 delimiter ;

 

 

 

函数randDate生成指定年份之间的任意年份:

 

 1 -- 随机生成一个1949年到1999之间的日期
 2 drop function if exists `randDate`;
 3 delimiter //
 4 create definer=`root`@`192.168.1.243` function `randDate`(n year) returns varchar(255) charset utf8
 5 begin
 6     declare aDate char(10) default '';
 7     set aDate=concat(n+floor((rand()*50)),'-',
 8                     lpad(floor(2 + (rand() * 11)),2,0),'-',
 9                     lpad(floor(3 + (rand() * 20)),2,0));
10     return aDate;
11 end//
12 delimiter ;

 

 

 

函数randDatetime生成任意时间点:

 

 1 -- 函数randDatetime:随机生成一个时间点函数
 2 drop function if exists `randDatetime`;
 3 delimiter //
 4 create definer=`root`@`192.168.1.243` function `randDatetime`(n year, num int) returns varchar(255) charset utf8
 5 begin
 6     declare aDatetime varchar(255) default '';
 7     set aDatetime=concat(concat(n+floor((rand()*num)),'-',
 8                     lpad(floor(2 + (rand() * 11)),2,0),'-',
 9                     lpad(floor(3 + (rand() * 25)),2,0)),
10                     ' ',
11                     concat(lpad(floor(0 + (rand() * 23)),2,0),':',
12                     lpad(floor(0 + (rand() * 60)),2,0),':',
13                     lpad(floor(0 + (rand() * 60)),2,0)));
14     return aDatetime;
15 end//
16 delimiter ;

 

 

 

 

posted @ 2018-02-02 10:06  哈哈你笑咯  阅读(2427)  评论(0编辑  收藏  举报