当前位置:网站首页>MySQL流程控制之while、repeat、loop循环实例分析
MySQL流程控制之while、repeat、loop循环实例分析
2022-07-29 03:11:00 【亿速云】
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 ));
-- -------存储过程-whiledelimiter $$create procedure proc16_while1(in insertcount int)begindeclare i int default 1;label:while i<=insertcount doinsert 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 + leavetruncate table user;delimiter $$create procedure proc16_while2(in insertcount int)begindeclare i int default 1;label:while i<=insertcount doinsert 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+iteratetruncate table user;delimiter $$create procedure proc16_while3(in insertcount int)begindeclare i int default 1;label:while i<=insertcount doset 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 [标签];
-- -------存储过程-循环控制-repeatuse mysql7_procedure;truncate table user;delimiter $$create procedure proc18_repeat(in insertCount int)begindeclare i int default 1;label:repeatinsert into user(uid, username, password) values(i,concat('user-',i),'123456');set i = i + 1;until i > insertCountend repeat label;select '循环结束';end $$delimiter ;call proc18_repeat(100);这个相对于是,无论如何都会执行一次的循环,然后是在内部进行判断,如果满足了就直接跳出
loop循环
loop循环体;if 条件表达式 thenleave [标签];end if;end loop;
-- -------存储过程-循环控制-looptruncate table user;delimiter $$create procedure proc19_loop(in insertCount int)begindeclare i int default 1;label:loopinsert into user(uid, username, password) values(i,concat('user-',i),'123456');set i = i + 1;if i > 5thenleave label;end if;end loop label;select '循环结束';end $$delimiter ;call proc19_loop(10);这个和repeat不同的是,需要执行之后,利用leave 跳出循环,无论是使用哪种都可以达到我们需要的效果,但是在业务中的应用场景,while还是相对比较的多。
以上就是“MySQL流程控制之while、repeat、loop循环实例分析”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
边栏推荐
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- sqlilabs less-32~less-33
- 西瓜书学习第六章---SVM
- Alibaba Sentinel - 工作流程及原理解析
- C traps and defects Chapter 3 semantic "traps" 3.4 avoid "couple method"
- Wechat's crazy use of glide - life cycle learning
- 【FreeSwitch开发实践】UniMRCP编译与安装
- C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
- 04 | 后台登录:基于账号密码的登录方式(上)
- MYSQL入门与进阶(十一)
猜你喜欢

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

「PHP基础知识」输出圆周率的近似值

Redis configuration cache expiration listening event trigger

【C】 Array

Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology

2022-07-28 顾宇佳 学习笔记

STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)

MySQL compound query (important)

July 28, 2022 Gu Yujia's study notes

13_ue4进阶_蒙太奇动画实现一边走一边攻击
随机推荐
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else
CentOS install mysql8
12_ue4进阶_换一个更好看的人物模型
Feedback function of conference OA
04 | 后台登录:基于账号密码的登录方式(上)
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
Principle knowledge is useful
VIM common commands
Multiline text omission
MySQL compound query (important)
会议OA之反馈功能
增量实时灾备笔记
Wechat's crazy use of glide - life cycle learning
数字图像处理 第10章——图像分割
A case of gradually analyzing the splitting of classes -- colorful ball collisions
shell脚本总结
Object转String的几种方法
VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
第09章_性能分析工具的使用
Detailed steps for installing MySQL 8.0 under Linux