`

MYSQL循环和退出条件

阅读更多
1.REPEAT

代码如下:
REPEAT
    Statements;
  UNTIL expression
END REPEAT
demo
DECLARE num INT;
DECLARE my_string  VARCHAR(255);
REPEAT
SET  my_string =CONCAT(my_string,num,',');
SET  num = num +1;
  UNTIL num < 5
END REPEAT;


2.WHILE

代码如下:

WHILE expression DO
    Statements;
END WHILE
demo
DECLARE num INT;
DECLARE my_string  VARCHAR(255);
SET num =1;
SET str ='';
  WHILE num<10 DO
SET  my_string =CONCAT(my_string,num,',');
SET  num = num +1;
END WHILE;



3.LOOP(这里面有非常重要的ITERATE,LEAVE)

代码如下:

DECLARE num  INT;
DECLARE str  VARCHAR(255);
SET num =1;
SET my_string ='';
  loop_label:  LOOP
IF  num <10THEN
      LEAVE  loop_label;
ENDIF;
SET  num = num +1;
IF(num mod3)THEN
      ITERATE  loop_label;
ELSE
SET  my_string =CONCAT(my_string,num,',');
ENDIF;
END LOOP;


PS:可以这样理解ITERATE就是我们程序中常用的contiune,而ITERATE就是break.当然在MySQL存储过程,需要循环结构有个名称,其他都是一样的.

官方的文档-->http://dev.mysql.com/doc/refman/5.0/en/loop.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics