流程控制

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

一.分支结构

1.1 if函数

功能:实现简单的双分支
语法:
select if(表达式1,表达式2,表达式3)
执行顺序:
如果表达式1成立,则if函数返回表达式2的值,否则返回表达式3的值
use myemployees;
select if(10<5,"大","小");
select last_name,commission_pct,if(commission_pct is null,"没奖金嘻嘻","有奖金呵呵") 备注 from employees;
应用:任何地方
View Code

1.2 case

情况1:类似于java中的switch语句,一般用于实现等值判断
语法:
    case 变量|表达式|字段
    when 要判断的值1 then 返回的值1
    when 要判断的值2 then 返回的值2
    when 要判断的值3 then 返回的值3
    ...
    else 要返回的值n
    end

例:
use myemployees;
select salary     原始工资,
       department_id,
       case department_id
           when 30 then salary * 1.1
           when 40 then salary * 1.2
           when 50 then salary * 1.3
           else salary
           end as 新工资
from employees;
    
情况2:类似于java中的多重if语句,一般用于实现区间判断
case 
when 要判断的条件1 then 返回的值1
when 要判断的条件2 then 返回的值2
when 要判断的条件3 then 返回的值3
...
else 要返回的值n或语句n;
end


特点:
    1.
    可以作为表达式,嵌套在其他语句中使用,可以放在任何地方,
    可以作为独立的语句去使用,只能放在begin end中
    2.
    如果when中值满足或条件成立,则执行对应的then后面的语句,并且结束case
    如果都不满足,则执行else中的语句或值
    3.
    else可以省略,如果else省略了,并且所有when条件都不满足,则返回null

#案例
#创建存储过程,根据传入的成绩,来显示等级,比如传入的成绩:90-100,显示A;80-90,显示B;60-80,显示C,否则,显示D
use myemployees;
delimiter $
create procedure test_case(in score int)
begin
    case
    when score>=90 then select 'A';
    when score>=80 then select 'B';
    when score>=60 then select 'C';
    else select "D";
    end case;
end $
View Code

2.if结构

功能:实现多重分支
语法:
    if 条件1 then 语句1;
    elseif 条件2 then 语句2;
    ...
    else 语句n;
注意:只能应用在begin end中
#案例
#创建存储过程,根据传入的成绩,来显示等级,比如传入的成绩:90-100,显示A;80-90,显示B;60-80,显示C,否则,显示D
delimiter $
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(50);
View Code

二、循环结构

分类:
  while、loop、repeat
循环控制:
  iterate类似于continue,结束本次循环,继续下一次
  leave类似于break,跳出,结束当前所在的循环

1.while

语法:
    
    [标签:] while 循环条件 do 
    循环体;
    end while [标签];
View Code

2.loop

语法:
    [标签:] loop
    循环体;
    end loop [标签];
可以用来模拟简单的死循环
View Code

3.repeat

语法:
    [标签:] repeat
    循环体;
    until 结束循环的条件
    end repeat [标签];
View Code

4.案例

4.1没有添加循环控制语句
#案例:批量插入,根据次数插入到admin表中多条记录
use girls;
delimiter $
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');
        set i=i+1;
    end while;
end $
call pro_while(100);

4.2添加循环控制语句
leave表示离开循环,相当于break
iterate则继续循环,相当于continue
leave语句
#案例2:批量插入,根据次数插入到admin表中多条记录,如果次数>20则停止
delimiter $
create procedure  test_while2(in insertCount int)
begin
    declare i int default 1;
    a:while i<=insertCount do
        insert into admin(username, password) values (concat("xiaohua",i),"0000");
        if i>=20 then leave a;
        end if;
        set i=i+1;
    end while a;
end $
call test_while2(50);


iterate语句
#案例:批量插入,根据次数插入到admin中多条记录,只插入偶数次
delimiter $
create procedure  test_while3(in insertCount int)
begin
    declare i int default 1;
    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("lixian",i),"9527");
    end while a;
end $
call test_while3(50);
View Code

5.练习

#1.已知表stringcontent其中字段:id自增长,content varchar(20)
#向该表插入指定个数的随机字符串
use girls;
drop table if exists stringcontent;
create table stringcontent(
    id int primary key auto_increment,
    content varchar(20)
);
delimiter $
create procedure test_randstr_insert(in insertCount int)
begin
    declare i int default 1;#定义一个循环变量i,插入次数
    declare str varchar(26) default "abcdefghijklmnopqrst";
    declare startIndex int default 1;#代表起始索引
    declare len int default 1;#代表截取的字符的长度
    while i<insertCount do
        set len=floor(rand()*(20-startIndex+1)+1);#产生一个随机的整数,代表截取长度,1-(26-startIndex+1)
        set startIndex=floor(rand()*26+1);#产生一个随机的整数,代表其实索引1-26
        insert into stringcontent(content) values (substr(str,startIndex,len));
        set i=i+1;#循环变量更新
        end while;
end $
call test_randstr_insert(20);
View Code
原文地址:https://www.cnblogs.com/xufengnian/p/11872199.html