MySQL 如何在MySQL中创建随机的四位数?

MySQL 如何在MySQL中创建随机的四位数?

首先创建一个表-

mysql> create table DemoTable717 (
   UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserPassword int
);
Query OK, 0 rows affected (0.81 sec)

使用insert命令将一些记录插入表中-

mysql> insert into DemoTable717(UserPassword) values(1454343);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable717(UserPassword) values(674654);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable717(UserPassword) values(989883);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable717(UserPassword) values(909983);
Query OK, 1 row affected (0.15 sec)

使用select语句显示表中的所有记录-

mysql> select *from DemoTable717;

这将产生以下输出-

+--------+--------------+
| UserId | UserPassword |
+--------+--------------+
| 1      | 1454343      |
| 2      | 674654       |
| 3      | 989883       |
| 4      | 909983       |
+--------+--------------+
4 rows in set (0.00 sec)

以下是创建一个随机的四位数并将其设置为“UserPassword”列的查询-

mysql> update DemoTable717
   SET UserPassword=(select floor(0+ RAND() * 10000));
Query OK, 4 rows affected (0.22 sec)
Rows matched: 4 Changed: 4 Warnings: 0

再次检查表记录-

mysql> select *from DemoTable717;

这将产生以下输出-

+--------+--------------+
| UserId | UserPassword |
+--------+--------------+
| 1      | 4319         |
| 2      | 3902         |
| 3      | 6553         |
| 4      | 1060         |
+--------+--------------+
4 rows in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程