介绍
顺序结构:程序从上至下依次执行
分支结构:程序从两条或多条路径中选择一条去执行
循环结构:程序在满足一定条件的基础上,重复执行一段代码
分支结构
一、if函数
功能:
实现简单的双分支
语法:
select if(表达式1, 表达式2, 表达式3);
执行顺序:
如果表达式1成立,返回表达式2的值,否则返回表达式3的值
应用场景:
任何地方
二、case函数
功能:
1、一般用于实现等值判断
2、一般用于实现区间判断(多重 if 判断)
特点:
① 可以作为表达式,嵌套在其它语句中使用,可放在任何地方,也可作为独
立的语句去使用,只能放在begin end中。
② 如果 when 中的值满足或者成立,则执行对应的then后面的语句,并且结
束case,如果都不满足,则执行else语句或值。
③ else可以省略,如果else省略了并且所有when条件都不满足则返回null
语法:
情况1:用于实现等值判断
case 变量|表达式|字段
when 要判断的值 then 返回值1或语句1
when 要判断的值 then 返回值2或语句2
.....
else 返回值n
end
情况2:用于实现区间判断
case
when 要判断的条件1 then 返回值1或语句1
when 要判断的条件2 then 返回值2或语句2
.....
else 返回值n
end
执行顺序:
如果表达式1成立,返回表达式2的值,否则返回表达式3的值
应用场景:
任何地方
三、if结构
功能:实现多重分支
语法:
if 条件1 then 语句1;
elseif 条件2 then 语句2;
......
else 语句n;
end if;
应用场景:
在存储过程或函数begin end 中。
循环结构
分类
while、loop、repeat
循环控制
iterate,跳出本次循环,继续下次循环
leave,结束当前循环
while
语法
【标签:】while 循环条件 do
循环体;
end while【标签】;
loop
语法
【标签:】loop
循环体;
end loop【标签】;
repeat
语法
【标签:】repeat
循环体;
until 结束循环的条件
end repeat【标签:】;
案例
1、批量插入,根据传入循环次数插入到 admin 表中多条记录
# 查询 admin 表信息 select * from admin; Empty set # 结果是空的,没有任何数据 # 创建存储过程 create procedure pro_while(in while_num int) begin declare i int default 1; while i <= while_num do insert into admin(username, `password`) values(concat('demo', i), concat('666', i)); set i = i + 1; end while; end # 执行存储过程 call pro_while(5); # 查询 admin 表信息 select * from admin; +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | demo1 | 6661 | | 2 | demo2 | 6662 | | 3 | demo3 | 6663 | | 4 | demo4 | 6664 | | 5 | demo5 | 6665 | +----+----------+----------+
2、批量插入,根据传入循环次数插入到 admin 表中多条记录,如果 次数 > 6 则停止
# 查询 admin 表信息 select * from admin; Empty set # 结果是空的,没有任何数据 # 创建存储过程 create procedure pro_while(in while_num int) begin declare i int default 1; flag:while i <= while_num do if i > 6 then leave flag; end if; insert into admin(username, `password`) values(concat('demo', i), concat('666', i)); set i = i + 1; end while flag; end # 执行存储过程 call pro_while(15); # 查询 admin 表信息 select * from admin; +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | demo1 | 6661 | | 2 | demo2 | 6662 | | 3 | demo3 | 6663 | | 4 | demo4 | 6664 | | 5 | demo5 | 6665 | | 6 | demo6 | 6666 | +----+----------+----------+
3、批量插入,根据传入循环次数插入到 admin 表中多条记录,只插入偶数次
# 查询 admin 表信息 select * from admin; Empty set # 结果是空的,没有任何数据 # 创建存储过程 create procedure pro_while(in while_num int) begin declare i int default 0; flag:while i <= while_num do set i = i + 1; if mod(i, 2) != 0 # i 对 2 取余 then iterate flag; end if; insert into admin(username, `password`) values(concat('demo', i), concat('666', i)); end while flag; end # 执行存储过程 call pro_while(15); # 查询 admin 表信息 select * from admin; +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | demo2 | 6662 | | 2 | demo4 | 6664 | | 3 | demo6 | 6666 | | 4 | demo8 | 6668 | | 5 | demo10 | 66610 | | 6 | demo12 | 66612 | | 7 | demo14 | 66614 | | 8 | demo16 | 66616 | +----+----------+----------+
4、repeat循环批量插入
# 创建 create procedure pro_while(in while_num int) begin declare i int default 0; flag:repeat insert into admin(username, `password`) values(concat('demo', i), concat('666', i)); set i = i + 1; until i > while_num end repeat flag; end # 调用 call pro_while(15);
5、loop循环批量插入
# 创建 create procedure pro_while(in while_num int) begin declare i int default 0; flag:loop insert into admin(username, `password`) values(concat('demo', i), concat('666', i)); set i = i + 1; if i > while_num then leave flag; end if; end loop flag; end # 调用 call pro_while(15);
循环结构总结
名称 | 语法 | 特点 | 位置 |
---|---|---|---|
while | lebel:while loop_condition do loop_listend while lebel; | 先判断后执行 | begin end中 |
repeat | label:repeat loop_listuntil end_conditionend repeat label; | 先执行后判断 | begin end中 |
loop | label:loop loop_listend loop label; | 没有条件的死循环 | begin end中 |
小练习
创建表 demo 其中字段包括:id、content
向该表插入指定个数的随机字符串
# 创建 demo 表 create table demo( id int(11) primary key auto_increment, contnet varchar(20) not null ) # 创建存储过程 create procedure insert_demo(in insert_count int) begin # 定义循环变量,表示插入次数 declare i int default 1; # 定义所有字符 declare str varchar(26) default 'abcdefghijklmnopqrstuvwxyz'; # 代表起始索引 declare start_index int default 1; # 代表截取的字符长度 declare len int default 1; # 实际插入内容 declare content varchar(20) default ''; while i <= insert_count do # 随机数,向下取整 rend()产生值 * 26 + 1 set start_index = floor(rand() * 26 + 1); # 产生随机整数,代表截取长度,1 - (26 - start_index + 1) set len = floor(rand() * (20 - start_index + 1) + 1); # 产生随机整数,代表起始索引1~26 set content = subsrt(str, start_index); insert into demo(`content`) value(content); # 循环变量更新 set i = i + 1; end while; end # 执行存储过程 call insert_demo(5); # 查询 demo 表 select * from demo; +----+-------------+ | id | content | +----+-------------+ | 1 | bcdefgh | | 2 | mnopqrs | | 3 | hij | | 4 | fghijk | | 5 | pqrstuvwxyz | +----+-------------+