zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    android 布局边框的使用,在xml文件中配置边框大小及颜色
    android去掉layout顶部的阴影(状态栏下边的阴影)
    ExpandableListView
    addTextChangedListener有错
    Android EditText____TextchangedListener
    LinkedHashMap.get("key")
    适配器模式(Java代码)
    单例模式(Java代码)
    工厂方法模式(Java代码)
    质数算法
  • 原文地址:https://www.cnblogs.com/zejin2008/p/5253054.html
Copyright © 2011-2022 走看看