在MySQL中我有一些表格,我需要将电话号码和电子邮件地址随机化,以便随机生成以用于开发目的。
在MySQL中,如何为电话号码生成7位唯一随机数?
如何生成545165498@mailinator.com
之类的随机电子邮件地址。
如何使用MySQL查询生成此随机数据?
答案 0 :(得分:6)
MySQL rand()
返回0
< = value
<范围内的随机浮点值。 1.0
。
用另一个数字乘以UPPER_BOUND
并得到它的最低点,你会得到一个介于0和(UPPER_BOUND-1)之间的随机整数,如下所示:
SELECT floor(rand() * 10) as randNum;
这只会给你一个0到10之间的随机数。
将10更改为比您想要生成的数字更高的数字。
这样的事情:
UPDATE user
SET email = CONCAT(FLOOR(rand() * 10000000),'@mailinator.com'),
PhoneNo = FLOOR(rand() * 10000000)
答案 1 :(得分: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')
答案 2 :(得分:4)
0(含)与1独占之间的随机数:
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5485130739850114 |
+--------------------+
1 row in set (0.00 sec)
0(包括)和10独占之间的随机int:
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个随机行加载到企鹅中。
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)
答案 3 :(得分:1)
这是一个生成具有许多选项的随机数据的在线工具。 http://www.generatedata.com/
只需输入参数即可定义所需的随机数据类型,并将其导出为适当的格式,然后即可加载。