创建生成五个随机数的 MySQL 存储过程?


借助以下查询,我们可以创建一个存储过程来生成五个随机数

mysql> DELIMITER //
mysql> DROP PROCEDURE IF EXISTS RandomNumbers;
    -> CREATE PROCEDURE RandomNumbers()
    -> BEGIN
    -> SET @i = 0;
    -> REPEAT
    -> SELECT RAND() AS 'Random Number';
    -> SET @i = @i + 1;
    -> UNTIL @i >=5 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.16 sec)

Query OK, 0 rows affected (0.16 sec)

现在,调用存储过程以获得结果

mysql> DELIMITER ;
mysql> CALL RandomNumbers();
+---------------------+
| Random Number       |
+---------------------+
| 0.25968261739209536 |
+---------------------+
1 row in set (0.00 sec)

+--------------------+
| Random Number |
+--------------------+
| 0.1659662734400167 |
+--------------------+
1 row in set (0.01 sec)

+---------------------+
| Random Number       |
+---------------------+
| 0.05078354575744229 |
+---------------------+
1 row in set (0.01 sec)

+--------------------+
| Random Number      |
+--------------------+
| 0.7560189392008064 |
+--------------------+
1 row in set (0.04 sec)

+------------------+
| Random Number    |
+------------------+
| 0.62774408946535 |
+------------------+
1 row in set (0.04 sec)

Query OK, 0 rows affected (0.05 sec)

更新于: 22-Jun-2020

545 已浏览

启动你的 职业

完成课程获得认证

入门
广告