Home  >  Article  >  Database  >  Mysql那些事儿之(十五)流程的控制_MySQL

Mysql那些事儿之(十五)流程的控制_MySQL

WBOY
WBOYOriginal
2016-06-01 13:38:52849browse

bitsCN.com


Mysql那些事儿之(十五)流程的控制

 

相关链接:

Mysql那些事儿之(一)mysql的安装

http:///database/201210/162314.html;

Mysql那些事儿之(二)有关数据库的操作

http:///database/201210/162315.html;

Mysql那些事儿之(三)有关数据表的操作

http:///database/201210/162316.html;

Mysql那些事儿之(四)数据表数据查询操作

http:///database/201210/162317.html;

Mysql那些事儿之(五)操作时间

http:///database/201210/162318.html;

Mysql那些事儿之(六)字符串模式匹配

http:///database/201210/163969.html;

Mysql那些事儿之(七)深入select查询

http:///database/201210/163970.html;

Mysql那些事儿之(八)索引

http:///database/201210/163971.html;

Mysql那些事儿之(九)常用的函数

http:///database/201210/164229.html;

Mysql那些事儿之(十)触发器一

http:///database/201210/164516.html;

Mysql那些事儿之(十一)触发器二

http:///database/201210/164766.html;

Mysql那些事儿之(十二)存储过程

http:///database/201210/164795.html;

Mysql那些事儿之(十三)变量、条件的使用

http:///database/201211/165662.html;

Mysql那些事儿之(十四)光标的使用

http:///database/201211/165664.html

 

IF语句    

Sql代码  

---语法结构  

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;  

CASE语句

Sql代码  

---CASE语句的语法格式  

CASE case_value  

    WHEN when_value THEN statement_list  

    [WHEN when_value THEN statement_list]....  

    [ELSE statement_list]  

END CASE  

   

---case语句举例:  

case  

  when i_staff_id = 2 then   

    set @x1 = @x1 + d_amount;  

  else  

    set @x2 = @x2 + d_amount;  

end case  

 LOOP语句

Sql代码  

[begin_label:] LOOP  

    statement_list  

END LOOP [end_label]  

---如果不在statement_list中增加退出循环的语句,那么LOOP语句可以用来实现简单的死循环。  

 LEAVE语句

Sql代码  

---将结束符替换为$$  

delimiter $$  

---创建存储过程  

CREATE PROCEDURE actor_num()  

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',222);  

  END LOOP ins;  

END;  

$$  

delimiter ;  

 ITERATE语句

Sql代码  

--必须用在循环中,作用就是跳过当前的循环直接进入下一轮循环。  

delimiter $$  

CREATE PROCEDURE actor_num()  

BEGIN  

  set @x = 0;  

  ins:LOOP  

    set @x = @x + 1;  

    IF @x = 100 THEN  

      leave ins;  

    ELSEIF mod(@x/2,0) = 0 THEN  

      iterate ins;  

    END IF;  

    INSERT INTO actor(first_name,last_name) VALUES('TEST',222);  

  END LOOP ins;  

END;  

$$  

delimiter ;  

 REPEAT 语句

Sql代码  

--有条件循环,当满足条件的时候退出循环。  

--语法:  

[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;  

 WHILE 语句

Sql代码  

---语法结构  

[begin_label:] WHILE search_condition DO  

    statement_list  

END WHILE [end_label]  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn