在MySQL中,我有一些表格需要为开发目的随机生成电话号码和电子邮件地址。
在MySQL中,如何生成7位数独特的随机电话号码?
如何生成像545165498@mailinator.com这样的随机电子邮件地址。
如何使用MySQL查询生成这些随机数据?
在MySQL中,如何生成7位数独特的随机电话号码?
如何生成像545165498@mailinator.com这样的随机电子邮件地址。
如何使用MySQL查询生成这些随机数据?
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)
生成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)
这应该给你一个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')
CONCAT
。 - tadman