MySQL 流程控制

1、MySQL 流程控制语句

  • MySQL 中可以使用 if、case、loop、leave、iterate、repeat 及 while 语句进行流程的控制。

1.1 if 语句

  • if 实现条件判断,满足不同的条件执行不同的语句列表。

    # if 语句
    
    # IF 
        search_condition 
      THEN 
        statement_list
      [ELSEIF search_condition THEN statement_list] ...
      [ELSE statement_list]
      END IF
    
    > if 
        i_staff_id = 2 
      then
        set @x1 = @x1 + d_amount;
      else
        set @x2 = @x2 + d_amount;
      end if;

1.2 case 语句

  • case 实现比 if 更复杂一些的条件构造。

    # case 语句
    
    # CASE
      WHEN 
        search_condition 
      THEN 
        statement_list
      [WHEN search_condition THEN statement_list] ...
      [ELSE statement_list]
      END CASE
    
    # CASE 
        case_value
      WHEN 
        when_value 
      THEN 
        statement_list
      [WHEN when_value THEN statement_list] ...
      [ELSE statement_list]
      END CASE
    
    > case
      when 
        i_staff_id = 2 
      then
        set @x1 = @x1 + d_amount;
      else
        set @x2 = @x2 + d_amount;
      end case;
    
    > case 
        i_staff_id
      when 
        2 
      then
        set @x1 = @x1 + d_amount;
      else
        set @x2 = @x2 + d_amount;
      end case;

1.3 loop 语句

  • loop 实现简单的循环,退出循环的条件需要使用其他的语句定义,通常可以使用 leave 语句实现。

    # loop 语句
    
    # [begin_label:] LOOP
        statement_list
      END LOOP [end_label]
  • 如果不在 statement_list 中增加退出循环的语句,那么 loop 语句可以用来实现简单的死循环。

1.4 leave 语句

  • leave 用来从标注的流程构造中退出,通常和 BEGIN ... END 或者循环一起使用。

  • 下面是一个使用 loop 和 leave 的简单例子,循环 100 次向 actor 表中插入记录,当插入 100 条记录后,退出循环。

    # leave 语句
    
    > create procedure actor_insert ()
      BEGIN
        set @x = 0;
      ins: LOOP
        set @x = @x + 1;
      IF 
        @x = 100 
      THEN
        LEAVE ins;
      END IF;
      INSERT INTO actor (first_name, last_name) VALUES ('Test', '201');
      END LOOP ins;
      END;
      $$
    
        Query OK, 0 rows affected (0.00 sec)
    
    > call actor_insert();
    
        Query OK, 0 rows affected (0.01 sec)
    
    > select count(*) from actor where first_name = 'Test';
    
        +----------+
        | count(*) |
        +----------+
        |      100 |
        +----------+
        1 row in set (0.00 sec)

1.5 iterate 语句

  • iterate 必须用在循环中,作用是跳过当前循环的剩下的语句,直接进入下一轮循环。

  • 下面的例子使用了 iterate 语句,当 @x 变量是偶数的时候,不再执行循环中剩下的语句,而直接进行下一轮的循环。

    # iterate 语句
    
    > CREATE PROCEDURE actor_insert ()
      BEGIN
        set @x = 0;
      ins: LOOP
        set @x = @x + 1;
      IF 
        @x = 10 
      THEN
        LEAVE ins;
      ELSEIF 
        mod(@x,2) = 0 
      THEN
        ITERATE ins;
      END IF;
      INSERT INTO actor(actor_id,first_name,last_name) VALUES (@x+200, 'Test',@x);
      END LOOP ins;
      END;
      $$
    
        Query OK, 0 rows affected (0.00 sec)
    
    > call actor_insert();
    
        Query OK, 0 rows affected (0.00 sec)
    
    > select actor_id,first_name,last_name from actor where first_name='Test';
    
        +----------+------------+-----------+
        | actor_id | first_name | last_name |
        +----------+------------+-----------+
        |      201 |       Test |         1 |
        |      203 |       Test |         3 |
        |      205 |       Test |         5 |
        |      207 |       Test |         7 |
        |      209 |       Test |         9 |
        +----------+------------+-----------+
        5 rows in set (0.00 sec)

1.6 repeat 语句

  • repeat 有条件的循环控制语句,当满足条件的时候退出循环。

    # repeat 语句
    
    # [begin_label:] REPEAT
        statement_list
      UNTIL 
        search_condition
      END REPEAT [end_label]
    
    > REPEAT
      FETCH cur_payment INTO i_staff_id, d_amount;
      if 
        i_staff_id = 2 
      then
        set @x1 = @x1 + d_amount;
      else
        set @x2 = @x2 + d_amount;
      end if;
      UNTIL 
        0 
      END REPEAT;

1.7 while 语句

  • while 实现的也是有条件的循环控制语句,即当满足条件时执行循环的内容。

  • while 循环和 repeat 循环的区别

    • while 是满足条件才执行循环,repeat 是满足条件退出循环;
    • while 在首次循环执行之前就判断条件,所以循环最少执行 0 次,而 repeat 是在首次执行循环之后才判断条件,所以循环最少执行 1 次。
    # while 语句
    
    # [begin_label:] WHILE search_condition DO
      statement_list
      END WHILE [end_label]
    
    > delimiter $$
    > CREATE PROCEDURE loop_demo ()
      BEGIN
        set @x = 1 , @x1 = 1;
      REPEAT
        set @x = @x + 1;
      UNTIL 
        @x > 0 
      END REPEAT;
    
      WHILE 
        @x1 < 0 
      DO
        set @x1 = @x1 + 1;
      END WHILE;
      END;
      $$
    
        Query OK, 0 rows affected (0.00 sec)
    
      > delimiter ;
      > call loop_demo();
    
        Query OK, 0 rows affected (0.00 sec)
    
      > select @x,@x1;
    
        +------+------+
        | @x   | @x1  |
        +------+------+
        |    2 |    1 |
        +------+------+
        1 row in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/QianChia/p/9240024.html