流程控制结构

顺序结构

分支结构

循环结构

#一、分支结构

#1.if函数

功能:实现简单的双分支

语法:

select if(表达式1,表达式2,表达式3)

执行顺序:

表达式1成立,返回表达式2的值,否则返回表达式3的值

应用:任何地方

#2.case结构

情况1.类似Java中的switch语句

语法:
case 变量或者表达式或者字段

when 要判断的值 then 返回的值

when 要判断的值 then 返回的值

when 要判断的值 then 返回的值

else 要返回的值

end

情况2.类似Java中的多重if

case 

when 要判断的条件 then 返回的值

when 要判断的条件 then 返回的值

when 要判断的条件 then 返回的值

else 要返回的值

end

二、循环结构

while.loop,repeat

循环控制:

#while

iterate类似于continue 结束本次循环,继续下一次

leave类似于break 跳出当前结构

语法:

【标签:】while 循环条件 do

  循环体;

end while【 标签】;

#loap

语法:

【标签:】loap

  循环体;

end loap【标签】;

#repeat

语法:

【标签:】repeat

  循环体;

until 结束循环的条件

end repeat【标签】;

 

#案例:批量插入,根据次数插入到admin表中多条记录

create procedure pro_while1(in insertCount int)

begin

  declare i int default 1;

  while i<=insertCount do

    insert into admin(username,password) values(xxx+i,xxx);

    set i = i+1;

  end while;

end $

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

truncate table admin$

drop procedure test_while1$

create procedure test_while1(in insertCount int)

begin

  declare i int default 1;

  a:while i<=insertCount do

    insert into admin(username,password) values(concat(xxx,i),0000) ;

    if i>20 then leave a;

    end if;

    set i=i+1;

  end while a;

end $

调用

call test_while1(100)$

 

posted @ 2022-12-19 20:45  平凡的柳先生  阅读(35)  评论(0)    收藏  举报