温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL流程控制while,repeat,loop循环实例分析

发布时间:2022-07-12 09:47:36 来源:亿速云 阅读:118 作者:iii 栏目:开发技术

本文小编为大家详细介绍“MySQL流程控制while,repeat,loop循环实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“MySQL流程控制while,repeat,loop循环实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

前言

  • 循环是一段在程序中只出现一次,但可能会连续运行多次的代码。

  • 循环中的代码会运行特定的次数,或者是运行到特定条件成立时结束循环。

MySQL流程控制while,repeat,loop循环实例分析

循环分类:

  • while

  • repeat

  • loop

循环控制:

leave 类似于 break,跳出,结束当前所在的循环

iterate类似于 continue,继续,结束本次循环,继续下一次

while循环

【标签:】while 循环条件 do
循环体;
end while【 标签】;
-- 创建测试表
create table user (
uid int primary_key,
username varchar ( 50 ),
password varchar ( 50 )
);
-- -------存储过程-while
delimiter $$
create procedure proc16_while1(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while(10);

存储过程语法是固定的:delimiter $$ create peocedure 循环名(参数)begin 代码 end $$ delimiter;

注意在写循环体的时候,必须要要有定义循环的初识变量,采用declare i int default 默认值

然后就是dlabel:while 判断条件 do 循环体 end while label; end && 必须要有

-- -------存储过程-while + leave
truncate table user;
delimiter $$
create procedure proc16_while2(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
if i=5 then leave label;
end if;
set i=i+1;
end while label;
end $$
delimiter ;
call proc16_while2(10);

如果在内部需要跳出循环的话,采用if 判断 ,但是最后需要end if 结尾

这里的leave就是 跳出循环,相对于break

-- -------存储过程-while+iterate
truncate table user;
delimiter $$
create procedure proc16_while3(in insertcount int)
begin
declare i int default 1;
label:while i<=insertcount do
set i=i+1;
if i=5 then iterate label;
end if;
insert into user(uid,username,`password`) values(i,concat('user-',i),'123456');
end while label;
end $$
delimiter ;
call proc16_while3(10);

这里的iterate 相对于continue 遇到就不执行下面的代码

repeat循环

repeat
循环体;
until 条件表达式
end repeat [标签];
-- -------存储过程-循环控制-repeat
use mysql7_procedure;
truncate table user;
delimiter $$
create procedure proc18_repeat(in insertCount int)
begin
declare i int default 1;
label:repeat
insert into user(uid, username, password) values(i,concat('user-',i),'123456');
set i = i + 1;
until i > insertCount
end repeat label;
select '循环结束';
end $$
delimiter ;
call proc18_repeat(100);

这个相对于是,无论如何都会执行一次的循环,然后是在内部进行判断,如果满足了就直接跳出

loop循环

 loop
循环体;
if 条件表达式 then
leave [标签];
end if;
end loop;
-- -------存储过程-循环控制-loop
truncate table user;

delimiter $$
create procedure proc19_loop(in insertCount int)
begin
declare i int default 1;
label:loop
insert into user(uid, username, password) values(i,concat('user-',i),'123456');
set i = i + 1;
if i > 5
then
leave label;
end if;
end loop label;
select '循环结束';
end $$
delimiter ;
call proc19_loop(10);

这个和repeat不同的是,需要执行之后,利用leave 跳出循环,无论是使用哪种都可以达到我们需要的效果,但是在业务中的应用场景,while还是相对比较的多。

读到这里,这篇“MySQL流程控制while,repeat,loop循环实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI