From b8e39099da23ce28af04d1d24529ba8f65b147b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8E=E5=81=A5?= <2161737470@qq.com> Date: Sun, 8 Oct 2023 20:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=B8=83=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\347\250\213\350\257\255\345\217\245.md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/20231008 \346\235\241\344\273\266\345\210\206\346\224\257\344\270\216\346\265\201\347\250\213\350\257\255\345\217\245.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20231008 \346\235\241\344\273\266\345\210\206\346\224\257\344\270\216\346\265\201\347\250\213\350\257\255\345\217\245.md" "b/04 \346\235\216\346\230\216\345\201\245/20231008 \346\235\241\344\273\266\345\210\206\346\224\257\344\270\216\346\265\201\347\250\213\350\257\255\345\217\245.md" new file mode 100644 index 0000000..41053db --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/20231008 \346\235\241\344\273\266\345\210\206\346\224\257\344\270\216\346\265\201\347\250\213\350\257\255\345\217\245.md" @@ -0,0 +1,138 @@ +### 条件分支语句 + +只能写在存储过程的 begin 和 end 中 + +**if 语句** + +语法: + +~~~ mysql +if 条件 then 执行语句; +else if 条件 then 执行语句; +else 执行语句; +end if; +~~~ + +**case 语句** + +语法: + +~~~ mysql +case when 条件 then 执行语句; +when 条件 then 执行语句; +else 执行语句; +end case; +~~~ + +### 流程语句-循环 + +**while 循环** + +举例:插入指定条数数据 + +~~~ mysql +# 循环插入n条数据 +delimiter $$ +create procedure proc11(in insertCount int) +begin + declare i int default 1; + label:while i <= insertCount do + insert into user(uid,username,psw) values(i,concat('user-',i),'123456'); + # if i = 5 then + # leave lable; 跳出循环 + # 或 + # iterate lable; 跳出本次循环,继续循环上面语句,此时就会死循环,i始终是5 + # end if; + set i = i + 1; + end while label; + select '循环结束'; +end $$ +delimiter ; +call proc11(10) #循环插入10条数据 +~~~ + +**loop(死循环)** + +举例:插入指定条数数据 + +~~~ mysql +delimiter $$ +create procedure proc13(in insertCount int) +begin + declare i int default 1; + label:loop + insert into user(uid,username,psw) values(i,concat('user-',i),'123456'); + set i = i + 1; + # 判断,当 i>10 的时候跳出循环,如果没有这一段,就是死循环 + /* if i > insertCount + then + leave label; 跳出循环 + end if; */ + end loop label; + select '循环结束'; +end $$ +delimiter ; +call proc12(10) +~~~ + +**作业** + +~~~ mysql +create database text01 charset utf8; +use text01; +-#1. 创建函数test_if_case(),实现传入成绩,如果成绩>90,返回A,如果成绩>80,返回B,如果成绩>60,返回C,否则返回D +#要求:分别使用if结构和case结构实现 + +#方式1:if + +delimiter $$ +create procedure test_if(in sorce double(4,1)) +begin + if sorce < 60 or sorce > 100 then select 'D' 等级; + elseif sorce <= 80 then select 'C' 等级; + elseif sorce <= 90 then select 'B' 等级; + else select 'A' 等级; + end if; +end $$ +delimiter ; + +call test_if(60) +call test_if(81) +call test_if(91) + +#方式2:case when ... +delimiter $$ +create procedure test_case(in sorce double(4,1)) +begin + case when sorce < 60 or sorce > 100 then select 'D' 等级; + when sorce <= 80 then select 'C' 等级; + when sorce <= 90 then select 'B' 等级; + else select 'A' 等级; + end case; +end $$ +delimiter ; + +call test_case(-1) +call test_case(150) + + +#2. 创建存储过程test_if_pro(),传入工资值,如果工资值<3000,则删除工资为此值的员工, +# 如果3000 <= 工资值 <= 5000,则修改此工资值的员工薪资涨1000,否则涨工资500 +drop procedure test_if_pro; +delimiter $$ +create procedure test_if_pro(in salary double(6,1)) +begin + set @salary = 0; + if salary < 3000 then select '该员工已删除'; + elseif salary >= 3000 and salary <= 5000 then set @salary = salary + 1000 ; + else set @salary = salary + 500; + end if; +end $$ + +call test_if_pro(2500); +call test_if_pro(3500); +select @salary; +call test_if_pro(6000); +select @salary; +~~~ + -- Gitee