02-017 MySQL_基础_流程控制结构

分类

1.顺序结构:程序从上到下依次执行
2.分支结构:程序从两条或多条路径中选择一条去执行
3.循环结构:在满足一定条件的基础上,重复执行一段代码

分支结构

1.if函数
功能:实现简单双分支
语法:select if(表达式1,表达式2,表达式3);
执行顺序:如果1成立,则返回2的值否则返回3
2.case
表达式或独立语句(这里主要讲作为独立语句)
情况1:类似switch 一般用于等值判断
case 变量|表达式|字段
when 要判断的值1 then 返回的值1或语句1;
when 要判断的值2 then 返回的值2或语句2;

else 要返回的值n或语句n;
end case;
情况2:类似多重if 一般用于区间判断
case
when 要判断的条件1 then 返回的值1或语句1;
when 要判断的条件2 then 返回的值2或语句2;

else 要返回的值n或语句n;
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(98);

3.多重if
语法:
if 条件1 then 语句1;
elseif 条件2 then 语句2;

【else 语句n;】
end if;

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

循环结构

while 、loop、repeat
循环控制:
iterate类似continue,继续,结束本次循环,继续下一次
leave 类似于break,跳出,结束当前所在循环
语法:
1.while
【标签:】while 循环条件 do
循环体;
end while【 标签】;
2.loop
【标签:】loop
循环体;
end loop 【标签】;
可以模拟死循环
3.repeat
【标签:】repeat
循环体;
until 结束循环的条件
end repeat 【标签】;
案例:批量插入,根据次数插入到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('lily',i),'123456');
		set i=i+1;
	end while;
end $
call test_while(10);

案例:批量插入,根据次数插入到admin表中多条记录,如果次数>20则停止

create procedure pro_while1(in insertCount int)
begin 
	declare i int default 1;
	a:while i<insertCount do
		insert into admin(username,password) values(concat('hua',i),'123456');
		if i>20 then leave  a;
		set i=i+1;
	end while a;
end $
call test_while1(100);

添加iterate语句

create procedure pro_while2(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('huah',i),'123456');
	end while a;
end $
call test_while2(100);

总结

放在 begin end 中

经典例题

向表中插入指定个数的随机字符串

#建表
drop table if exists stringcontent;
create table stringcontent)
	id int primary key default 1,
	content varchar(20)
);
#向该表插入指定个数的随机字符串
delimiter $
create procedure test_randstr_insert(in insertCount int)
begin
	declare i int default 1;#定义一个循环变量
	declare str varchar(26) default 'abcdefghijklmnopqrstuvwxyz,;
	declare startIndex int default 1;#起始索引
	declare len int default 1;#字符串截取长度
	while i<insertCount do
		set len = floor(rand()*(20-startIndex+1)+1);
		set startIndex = floor(rand()*26+1);#随机整数
		insert into stringcontent(content) values(substr(str,startIndex,len));
		set i=i+1;#循环变量更新
	end while;
end $

学习整理于MySQL 基础+高级篇.

发布了53 篇原创文章 · 获赞 0 · 访问量 385

猜你喜欢

转载自blog.csdn.net/weixin_40778497/article/details/103644262