mysql实例 学习while循环的用法

发布时间:2019-08-13编辑:脚本学堂
本文介绍下,mysql的一个例子,有关while循环的用法。

mysql中while循环的例子,代码如下:
例1,

mysql> delimiter //
mysql> create procedure test_while (IN in_count INT) //创建存储过程 学习while循环的用法
    -> BEGIN
    ->     declare count INT default 0;
    ->
    ->     while count < 10 do
    ->         set count = count + 1;
    ->     end while;
    ->
    ->     select count;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call test_while(10);
+-------+
| count |
+-------+
|    10 |
+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> drop procedure test_while; //删除存储过程
Query OK, 0 rows affected (0.00 sec)

例2,while语句示例

mysql> DELIMITER //
mysql> CREATE FUNCTION myFunction (quantity INT(10)) RETURNS INT(10) //创建存储过程 学习while循环语句
    -> BEGIN
    ->
    ->     WHILE quantity MOD 12 > 0 DO
    ->     SET quantity = quantity + 1;
    ->     END WHILE;
    ->
    ->     RETURN quantity;
    ->
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> select myFunction(10);
+----------------+
| myFunction(10) |
+----------------+
|             12 |
+----------------+
1 row in set (0.00 sec)

mysql>
mysql> select myFunction(24);
+----------------+
| myFunction(24) |
+----------------+
|             24 |
+----------------+
1 row in set (0.00 sec)

mysql>
mysql> drop function myFunction;
Query OK, 0 rows affected (0.00 sec)