From 13e7d1568da019938a5e601f3f1969d807229c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?16=20=E9=98=99=E8=8B=8F=E6=96=87?= <2361635242@qq.com> Date: Mon, 9 Oct 2023 22:44:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\346\224\257\347\273\223\346\236\204.md" | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 "16 \351\230\231\350\213\217\346\226\207/20231008\345\210\206\346\224\257\347\273\223\346\236\204.md" diff --git "a/16 \351\230\231\350\213\217\346\226\207/20231008\345\210\206\346\224\257\347\273\223\346\236\204.md" "b/16 \351\230\231\350\213\217\346\226\207/20231008\345\210\206\346\224\257\347\273\223\346\236\204.md" new file mode 100644 index 0000000..5b99fcf --- /dev/null +++ "b/16 \351\230\231\350\213\217\346\226\207/20231008\345\210\206\346\224\257\347\273\223\346\236\204.md" @@ -0,0 +1,94 @@ +# 笔记 + +1.分支结构:if语法结构 + +格式:IF 表达式1 THEN 操作1 sql语句 +[ELSEIF 表达式2 THEN 操作2]…… +[ELSE 操作N] +END IF + +特点:① 不同的表达式对应不同的操作 ② 使用在begin end中 + +2.分支结构: CASE + +格式:CASE 表达式 +WHEN 值1 THEN 结果1或语句1(如果是语句,需要加分号) +WHEN 值2 THEN 结果2或语句2(如果是语句,需要加分号) +... +ELSE 结果n或语句n(如果是语句,需要加分号) +END [case](如果是放在begin end中需要加上case,如果放在select后面不需要) + +3.循环结构:loop + +格式:[loop_label:] LOOP +循环执行的语句 +END LOOP [loop_label] + +4.循环结构:while + +格式:[while_label:] WHILE 循环条件 DO +循环体 +END WHILE [while_label]; + +# 作业 + +```mysql +#1. 创建函数test_if_case(),实现传入成绩,如果成绩>90,返回A,如果成绩>80,返回B,如果成绩>60,返回C,否则返回D +#要求:分别使用if结构和case结构实现 + +#方式1:if +create database xc charset utf8; +use xc; + +drop procedure test_if_case; +delimiter // +create procedure test_if_case(in crea double(4,2)) +BEGIN +if crea>90 then select 'A'; +elseif crea>80 then select 'B'; +elseif crea>60 then select 'C'; +else select 'D'; +end if; +end // +delimiter ; + +-- 调用 + +call test_if_case(-1); + +#方式2:case when ... +delimiter // +create procedure testtest_if_case(in crea double(4,2)) +BEGIN + case + when crea >90 then select 'A'; + when crea >80 then select 'B'; + when crea >60 then select 'C'; + else select 'D'; + end case; +end// +delimiter; + +-- 调用 +call test_if_case(-1); + + +#2. 创建存储过程test_if_pro(),传入工资值,如果工资值<3000,则删除工资为此值的员工, +# 如果3000 <= 工资值 <= 5000,则修改此工资值的员工薪资涨1000,否则涨工资500 +delimiter // +create procedure test_if_pro(in sly double(6,2)) +begin +if sly <3000 then + delete from employees where salary=sly; + elseif + 3000<=sly and sly<=5000 then + update employees set salary = salary+1000 where salary=sly; + else + update employees set salary=salary+500 where salary=sly; + end if; +end //; +-- 调用 +call test_if_pro(4000); + +``` + -- Gitee