教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 mysql实例 循环结构中使用if语句

mysql实例 循环结构中使用if语句

发布时间:2016-01-01   编辑:jiaochengji.com
本文介绍下,一个mysql循环结构的例子,其中用到了if语句,有需要的朋友参考下吧。

本代码在mysql的循环结构中应用if语句,供大家参考。

代码:

mysql> delimiter $$
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->
    ->     DECLARE i int;
    ->     SET i=0;
    ->     loop1: REPEAT
    ->          SET i=i+1;
    ->          IF MOD(i,2)<>0 THEN /*Even number - try again*/
    ->             SELECT CONCAT(i," is an odd number");
    ->          END IF;
    ->     UNTIL i >= 10
    ->     END REPEAT;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call myProc();
+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 1 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 3 is an odd number            |
+-------------------------------+
1 row in set (0.33 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 5 is an odd number            |
+-------------------------------+
1 row in set (0.33 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 7 is an odd number            |
+-------------------------------+
1 row in set (0.34 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 9 is an odd number            |
+-------------------------------+
1 row in set (0.34 sec)

Query OK, 0 rows affected (0.34 sec)

mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

您可能感兴趣的文章:
mysql实例 循环结构中使用if语句
mysql实例 while循环中的if语句
Python break用法详解
mysql存储过程中的三种循环
c#分支与循环结构的实例解析
MySQL 存储过程中使用 WHILE 循环语句的例子
linux shell学习之shell流程控制
python中如何退出for循环
awk基础知识(10)-循环结构
php break跳出多重循环实例

[关闭]
~ ~