如何在 MySQL 中建立一個四位隨機數?
我們首先建立一個表 -
mysql> create table DemoTable717 ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserPassword int ); Query OK, 0 rows affected (0.81 sec)
使用插入命令在表中插入一些記錄 -
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)
以下是查詢,隨機建立 4 位數並將其設定為 “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)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP