• mysql 随机字符的产生方法


    需求:需要插入随机数据,长度为6位,包含数字和大写字母。

    一般来说我们会写类似如下的存储过程片断:

       declare str char(62) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
       declare str6 char(6);
    
       set str6=concat(substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1),substring(str,1+floor(rand()*61),1));
    

    其实我们也可以采用下面另一种方法,用char函数,产生单一随机字符的方法如下:

    select char(if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)))

    当然要产生6位的话就直接复制多几个出来就好了:

    select char(if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)),if(floor(rand()*2)=0,65+floor(rand()*26),48+floor(rand()*9)))

    小记录一下

    以下产生的是符合位数的,不一定有意义,满足一些测试要求:

    随机身份证:

    concat('44030319',cast(UNIX_TIMESTAMP()+floor(rand()*8000000000) as char )),

    随机手机号:

    UNIX_TIMESTAMP()*10+floor(rand()*4000000000)
  • 相关阅读:
    pandas 之 数据合并
    第一册:lesson sixty five.
    第一册:lesson sixty three。
    C#正则表达式。
    第一册:lesson sixty one.
    C#泛型。
    SQL命令入门。
    C#序列化与反序列化。
    第一册:lesson fifty nine。
    C#文件操作。
  • 原文地址:https://www.cnblogs.com/zejin2008/p/5253054.html
Copyright © 2020-2023  润新知