【MySQL】流程控制语句

分支结构


case 结构

  1. 作为表达式 → 嵌套在其他语句中使用 → 可以放在任何地方
  2. 作为独立的语句去使用 → 只能放在 begin end

用法1:效果参考 switch … case … 语句

case 字段|表达式|变量
when 常量1 then 返回值1
when 常量2 then 返回值2
else 默认返回值 -- else 可以省略
end -- 没有满足的条件则返回 null

eg:查询读者性别

select readerName, sex,
    case sex
    when '男' then '是个爷们'
    when '女' then '是个姑娘'
    else '未知'
    end as 'gender'
from reader;

用法2:效果参考 if … else if … else … 语句

case
when 条件1 then 语句1;
when 条件2 then 语句2;
else 语句n; -- else 可以省略
end case; -- 没有满足的条件则返回 null

eg:创建存储过程,根据传入的成绩来显示等级

drop procedure if exists testCase;
delimiter $
create procedure testCase(in score int)
begin
	case
	when score>=90 then select '优秀' as '成绩';
	when score>=80 then select '良好' as '成绩';
	when score>=60 then select '及格' as '成绩';
	else select '不及格' as '成绩';
	end case;
end $

call testCase(95);

if 结构

用法1:函数用法,效果参考三元运算符

if(判断语句, 判断为ture返回, 判断为false返回)

eg:判断 5<10,为 ture 则返回第二个参数,为 false 则返回第三个参数

select if(5<10, '大', '小'); -- 大

用法2:参考 if … elseif … else … 语句

只能应用在 begin end

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

eg:创建函数,根据传入的成绩来显示等级

drop function if exists testIf;
delimiter $
create function testIf(score int) returns char(3)
begin
	if score>=90 then return '优秀';
	elseif score>=80 then return '良好';
	elseif score>=60 then return '及格';
	else return '不及格';
	end if;
end$

select testIf(66) as '成绩';

循环结构

注意:只能放在 begin end 语句中

循环控制

  1. iteratecontinue
  2. leavebreak

while 语句

先判断,后执行

while 循环条件 do
	循环体;
end while;

如果要搭配循环控制,要给循环语句起标签名

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

eg:while do 实现批量插入

drop procedure if exists pro_while;
delimiter $
create procedure pro_while(in insertCount int)
begin
	declare i int default 1; -- 声明并初始化局部变量 i
	while i <= insertCount do
		insert into reader(readerNo, readerName, sex) values(i, concat(i, '号读者'), '男');
		set i = i + 1; -- 循环变量更新
    end while;
end$

call pro_while(5); -- 插入 5 条记录

loop 语句

[标签:] loop
	循环体;
end loop [标签];

loop 没有 循环条件,所以要搭配 循环控制 使用;否则就是死循环

eg:loop 实现批量插入

drop procedure if exists pro_while;
delimiter $
create procedure pro_while(in insertCount int)
begin
	declare i int default 1; -- 声明并初始化局部变量 i
	halt: loop
		insert into reader(readerNo, readerName, sex) values(i, concat(i, '号读者'), '男');
         if i >= insertCount then leave halt; -- leave 控制语句
         end if;
		set i = i + 1; -- 循环变量更新
    end loop halt;
end$

call pro_while(5); -- 插入 5 条记录

repeat

先执行后判断

[标签:] repeat
	循环体;
until 结束循环的条件 -- 注意这里没有 ;
end repeat [标签];

类比 do ... while ...

eg:repeat实现批量插入

drop procedure if exists pro_while;
delimiter $
create procedure pro_while(in insertCount int)
begin
	declare i int default 1; -- 声明并初始化局部变量 i
	repeat
		insert into reader(readerNo, readerName, sex) values(i, concat(i, '号读者'), '男');
		set i = i + 1; -- 循环变量更新
	until i >= insertCount
    end repeat;
end$

call pro_while(5); -- 插入 4 条记录
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JS.Huang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值