mysql数据库(7)--流程控制函数

一、分支结构

1、if函数:可以实现if-else的效果

1 select if(commission_pct is Null,"没奖金","有奖金") as 备注

2、case函数

方式一:实现switch-case的效果 -- 适用于等值判断

1 case 要判断的字段或表达式
2 when 常量1 then 要显示的值1或者语句1;   # 如果是值,就不用;
3 when 常量2 then 要显示的值2或者语句2;
4 ......
5 else 要显示的值n或语句n
6 end [case]     # 如果是值就是用end,如果是语句就使用end case

方式二:实现多重if的效果 -- 适用于区间判断

1 case
2 when 条件1 then  要显示的值1或者语句1;
3 when 条件2 then  要显示的值2或者语句2;
4 ......
5 else 要显示的值n或者语句n
6 end [case]    # 如果是值就是用end,如果是语句就使用end case

 说明:

如果then后面是要显示的值,则可以放置在sql中的任意位置;

如果then后面是要显示的语句,则一般放置在begin end结构中

 1 # 要求:创建存储过程,根据输入的成绩显示等级
 2 
 3 create procedure test_case(in sore int)
 4 begin 
 5        case
 6        when score >=90 and score <=100 then select 'A';
 7        when score >=80 then select 'B'; 
 8        when score >=60 then select 'C'; 
 9        else select 'D';
10        end case
11 end $
12 
13 # 存储过程的调用
14 call test_case(95)$

3、if结构  --  应用在begin end结构中

1 if 条件1 then 语句1;
2 elseif 条件2 then 语句2;
3 ......
4 else 语句n;
5 end if;

【应用】

 1 # 根据输入的成绩,返回等级   -- 使用函数
 2 
 3 create function test_if(score int) returns char
 4 begin
 5     if score >=90 and score <= 100 then return 'A';
 6     elseif score >= 80 then return 'B';
 7     elseif score >= 60 then return 'C' 8     else return 'D';
 9     end if;
10 end$

二、循环结构

1、分类

while、loop、repeat  -- 只能放置在begin end结构中

iterate:类似continue,用于结束本次循环

leave:类似break,用于跳出循环

2、语法结构

(1)while

[标签:]while 循环条件 do
    循环体;
end while[标签];

(2)loop(死循环)

1 [标签:]loop
2     循环体;
3 end loop[标签]

(3)repeat

1 [标签:]repeat
2     循环体;
3 until 结束循环的条件
4 end repeat[标签];

【说明】添加标签的目的是为了便于对循环进行控制

3、案例分析

【案例1】使用循环批量插入多条数据到admin表中

 1 create procedure pro_while(in insertCount int)
 2 begin
 3      declare i int default 1;
 4      while i <= insertCount do
 5          insert into admin(username,password) values(concat('yif',i),'666');
 6          set i = i + 1;
 7      end while;
 8 end $
 9 
# 调用存储过程 10 call pro_while(100)$

【案例2】使用循环批量插入前20条数据到admin表中

create procedure test_leave(in insertCount int)
begin
      declare i int default 1;
      a:while i <= insertCount do
           insert into admin(username,pasword) values(concat('yif',i),'000');
           if i > 20 then leave a;
           end if
           set i = i + 1;
      end while a;
end $

call test_leave(100)$

【补充案例】向表中插入指定个数的随机字符串

 1 delimiter $
 2 create procedure test_randstr_insert(in insertCount int)
 3 begin
 4        declare i int default 1;
 5        declare str varchar(26) default 'abcdefghijklmnopqrstuvwxyz';
 6        declare start_index int default 1;
 7        declare len int default 1;
 8        while i <= insertCount do
 9               set len = floor(Rand()*(26-start_index+1)+1);    # 产生一个随机整数,代表截取长度
10               set start_index = floor(Rand()*26+1);         # 产生一个随机的整数,代表起始索引
11               insert into stringcontent(content) values (substr(str,start_index,len));  # 向数据库中插入子串
12               set i = i + 1;
13        end while;
14 end $
15 
16 call test_randstr_insert(10)$
原文地址:https://www.cnblogs.com/yif930916/p/15035850.html