在 MySQL 中向表內插入隨機數?
要插入隨機數,請使用 MySQL 的 RAND() 函式。首先,讓我們建立一個表 -
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.46 sec)
使用 insert 命令向表中插入一些記錄 -
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.08 sec)
使用 select 語句顯示錶中的所有記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 -
+-------+ | Value | +-------+ | 10 | | 20 | | 30 | +-------+ 3 rows in set (0.00 sec)
讓我們向 MySQL 中的一個表插入隨機數 -
mysql> update DemoTable set Value=FLOOR(@number * rand()) + 1; Query OK, 3 rows affected (0.17 sec) Rows matched: 3 Changed: 3 Warnings: 0
讓我們再次檢查表記錄 -
mysql> select *from DemoTable;
這將產生以下輸出 -
+-------+ | Value | +-------+ | 2 | | 2 | | 1 | +-------+ 3 rows in set (0.00 sec)
廣告