MySQL生成随机数据

8
在MySQL中,我有一些表格需要为开发目的随机生成电话号码和电子邮件地址。
在MySQL中,如何生成7位数独特的随机电话号码?
如何生成像545165498@mailinator.com这样的随机电子邮件地址。
如何使用MySQL查询生成这些随机数据?

只需使用MySQL字符串函数,如CONCAT - tadman
4个回答

10

MySQL rand()函数返回一个在范围 0 <= value < 1.0 内的随机浮点数。

将该值乘以另一个数:UPPER_BOUND,取其整数部分,即可得到一个介于 0 和 (UPPER_BOUND-1) 之间的随机整数,如下所示:

SELECT floor(rand() * 10) as randNum;

这将会给你一个0到10之间的随机数。

将10改为比你想生成的数字高1的数字。

就像这样:

UPDATE user 
SET email = CONCAT(FLOOR(rand() * 10000000),'@mailinator.com'), 
    PhoneNo = FLOOR(rand() * 10000000)

这个解决方案唯一的问题(对你来说可能不是问题)就是它不总是生成7位随机数。这个随机数生成器将会生成0到10000000之间的数字。这意味着例如数字123可能会被生成,那么你的邮件地址将是123@mailinator.com而不是0000123@mailinator.com - Dewald Swanepoel

5

MySQL生成随机数据步骤:

生成0(包含)到1(不包含)之间的随机数:

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.5485130739850114 |
+--------------------+                                                     
1 row in set (0.00 sec)

生成介于0(含)和10(不含)之间的随机整数:

mysql> select floor(rand()*10);
+------------------+
| floor(rand()*10) |
+------------------+
|                6 |
+------------------+
1 row in set (0.00 sec)

随机字母或数字:

mysql> select concat(substring('ABCDEF012345', rand()*36+1, 1));
+---------------------------------------------------------------------------+
| concat(substring('ABCDEF012345', rand()*36+1, 1))                         |
+---------------------------------------------------------------------------+
| F                                                                         |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)

随机字母 a 到 z:

mysql> select char(round(rand()*25)+97);
+---------------------------+
| char(round(rand()*25)+97) |
+---------------------------+
| s                         |
+---------------------------+
1 row in set (0.00 sec)

随机生成一个由8个字母和数字组成的字符串:

mysql> SELECT LEFT(UUID(), 8);
+-----------------+
| LEFT(UUID(), 8) |
+-----------------+
| c26117af        |
+-----------------+
1 row in set (0.00 sec)

在MySQL中生成随机大写字母:

mysql> select CHAR( FLOOR(65 + (RAND() * 25)));
+----------------------------------+
| CHAR( FLOOR(65 + (RAND() * 25))) |
+----------------------------------+
| B                                |
+----------------------------------+
1 row in set (0.00 sec)

在表格中加载随机行:

mysql> create table penguin (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into penguin values (0, LEFT(UUID(), 8));
Query OK, 1 row affected (0.00 sec)

mysql> select * from penguin;
+------+----------+
| id   | msg      |
+------+----------+
|    0 | abab341b |
+------+----------+
1 row in set (0.00 sec)

加载随机行:

创建一个名为“dennis”的过程,将1000个随机行加载到penguin中。

mysql> delimiter ;;
mysql> drop procedure if exists dennis;;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> create procedure dennis()
    -> begin
    -> DECLARE int_val INT DEFAULT 0;
    -> myloop : LOOP
    ->   if (int_val = 1000) THEN
    ->     LEAVE myloop;
    ->   end if;
    ->   insert into penguin values (0, LEFT(UUID(), 8));
    ->   set int_val = int_val +1;
    -> end loop;
    -> end;;
Query OK, 0 rows affected (0.00 sec)  

mysql> call dennis();;

mysql> select * from penguin;;
+------+----------+                      
| id   | msg      |              
+------+----------+                   
|    0 | abab341b |                 
|    1 | c5dc08ee |           
|    2 | c5dca476 |
...
+------+----------+

更新表中所有行的数据为随机数据:

mysql> create table foo (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into foo values (0,'hi');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values (0,'hi2');
Query OK, 1 row affected (0.00 sec)

mysql> insert into foo values (0,'hi3');
Query OK, 1 row affected (0.00 sec)

mysql> select * from foo;
+----+------+
| id | msg  |
+----+------+
|  1 | hi   |
|  2 | hi2  |
|  3 | hi3  |
+----+------+
3 rows in set (0.00 sec)

mysql> update foo set msg = rand();
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from foo;
+----+---------------------+
| id | msg                 |
+----+---------------------+
|  1 | 0.42576668451145916 |
|  2 | 0.6385560879842901  |
|  3 | 0.9154804171207178  |
+----+---------------------+
3 rows in set (0.00 sec)

5

这应该给你一个7位数的随机数字

SELECT FLOOR(1000000 + RAND() * 8999999)

类似于这样,根据您的要求更新电话号码和电子邮件地址

UPDATE Customers 
SET phone = CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR), 
email = CONCAT(CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR), '@mailinator.com')

以下是没有使用varcar转换的代码(因为它在MySQL和其他一些数据库中无法工作),而且RAND(8999999)部分也是错误的(应该乘以8999999): UPDATE Customers SET phone=FLOOR(1000000 + RAND()*8999999), email=CONCAT(FLOOR(1000000 + RAND() * 8999999), '@example.com')。 - orrd

2
这里有一个在线工具,可以生成各种随机数据,并有多种选项。链接如下:http://www.generatedata.com/。只需输入参数来定义所需的随机数据类型,然后将其导出到适当的格式,就可以加载它了。

非常感谢,这正是我正在寻找的:D - Gonzalo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接