声明:此MySQL基础学习源自尚硅谷。(推荐)b站官方链接:https://www.bilibili.com/video/BV1xW411u7ax?p=1
流程控制结构
| 顺序结构:程序从上往下依次执行 |
| 分支结构:程序从两条或多条路径中选择一条去执行 |
| 循环结构:程序在满足一定条件的基础上,重复执行一段代码 |
1.分支结构
1.if函数
| 功能:实现简单的双分支 |
| 语法: |
| if(表达式1,表达式2,表达式3) |
| |
| 执行顺序: |
| 如果表达式1成立,则if函数返回表达式2的值,否则返回表达式3的值 |
| 应用:任何地方 |
2.case结构
| 情况1:类似switch语句,一般用于实现等值判断 |
| 语法: |
| case 变量|表达式|字段 |
| when 要判断的值 then 返回的值1或(语句1;) # 若是值没有; 若是语句需加; |
| when 要判断的值 then 返回的值2或(语句2;) |
| when 要判断的值 then 返回的值3或(语句3;) |
| ... |
| else 要判断的值n then 返回的值n或(语句n;) |
| end case; |
| 情况2:类似多重if语句,一般用于实现区间判断 |
| 语法: |
| case |
| when 要判断的条件1 then 返回的值1或(语句1;) |
| when 要判断的条件2 then 返回的值2或(语句2;) |
| ... |
| else 要判断的条件n then 返回的值n或(语句n;) # else 可以省略 |
| end case; |
| 特点: |
| ①可以作为表达式,嵌套在其他语句中使用,可以放在任何地方,begin end 中或begin end 的外面 |
| 可以作为独立的语句去使用,只能放在begin end 中。 |
| ②如果when中的值满足或条件成立,则执行对应的then后面的语句,并且结束case |
| 如果都不满足,则执行else中的语句或值 |
| ③else可以省略,如果else省略了,并且所有when条件都不满足,则返回null |
表达式:

独立语句:

案例:创建存储过程,根据传入的成绩来显示等级,比如传入的成绩:90-100显示A,80-90显示B,60-80显示C
否则显示D
| create procedure test_case(in score int) |
| begin |
| case |
| when score>=90 and score<=100 then select 'A'; |
| when score>=80 then select 'B'; |
| when score>=60 then select 'C'; |
| else select 'D'; |
| end case; |
| |
| end $ |
| |
| call test_case(95)$ |
3.if结构
| 功能:实现多重分支 |
| 语法: |
| if 条件1 then 语句1; |
| elseif 条件2 then 语句2; |
| ... |
| 【else 语句n;】 |
| end if; |
| |
| 应用在 begin end 中 |
案例:根据传入的成绩,来显示等级,比如传入的成绩:90-100,返回A,80-90,返回B,60-80,返回C,否则返回D
| create function test_if(score int) returns char |
| begin |
| if score>=90 and score<=100 then return 'A'; |
| elseif score>=80 then return 'B'; |
| elseif score>=60 then return 'C'; |
| else return 'D'; |
| end if; |
| |
| end $ |
| |
| select test_if(86)$ |
2.循环结构
| 分类: |
| while、loop、repeat |
| |
| 循环控制: |
| iterate 类似于 continue,结束本次循环,继续下一次循环 |
| leave 类似于 break,结束当前所在的循环 |
1.while
| 语法: |
| 【标签:】 while 循环条件 do |
| 循环体; |
| end while 【标签】; |
2.loop
| 语法: |
| 【标签:】 loop |
| 循环体; |
| end loop 【标签】; |
| |
| |
3.repeat
| 语法: |
| 【标签:】 repeat |
| 循环体; |
| until 结束循环的条件 |
| end repeat 【标签】; |
1.没有添加循环控制语句
案例:批量插入,根据次数插入到admin表中多条记录
| create procedure pro_while(in insertCount int) |
| begin |
| declare i int default 1; |
| while i<=insertCount do |
| insert into admin(`username`,`password`) values(concat('Rose',i),'666'); |
| end while; |
| |
| end $ |
2.添加leave语句
案例:批量插入,根据次数插入到admin表中多条记录,如果次数>20则停止
| create procedure test_while(in insertCount int) |
| begin |
| declare i int default 1; |
| a:while i<=insertCount do |
| insert into admin(`username`,`password`) values(concat('xiaohua',i),i); |
| if i>=20 then leave a; # leave后面必须有结束的位置名 |
| end if; |
| set i = i+1; |
| end while a; |
| |
| end $ |
3.添加iterate语句
案例:批量插入,根据次数插入到admin表中多条记录,只插入偶数次
| create procedure test_while(in insertCount int) |
| begin |
| declare i int default 0; |
| a:while i<=insertCount do |
| set i=i+1; |
| if mod(i,2)!=0 then iterate a; |
| end if; |
| |
| insert into admin(`username`,`password`) values(concat('xiaoming',i),i); |
| |
| end while a; |
| end $ |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步