mysql循环语句(loop、while ....do..、repeat),

此篇文章是通过存储过程来介绍的

loop循环语法格式:

标签名:loop

// 要循环的语句块

leave  标签名  //退出循环

end   loop;

示例:

create procedure p12(a INT,out b INT)
BEGIN
declare i int DEFAULT 1;

aa:LOOP
SET b=i+a;
IF b>a THEN
leave aa;
END if;
END LOOP;
SELECT b;
END;

while 语句

语法格式:

[标签]while 条件 do 要循环的内容

end while;


示例:

create procedure p12(a INT,out b INT)
BEGIN
declare i int DEFAULT 1;

while b SET b=i+a;

END while;
SELECT b;
END;

repeat 循环

语法格式:

repeat  循环语句  until 条件   end repeat;

示例:

create procedure p12(a INT,out b INT)
BEGIN
declare i int DEFAULT 1;
repeat SET b=i+a; until b>a
END repeat;
SELECT b;
END;



你可能感兴趣的:(mysql)