Skip to content

Commit 8a342be

Browse files
committed
add: 流程控制
1 parent 0b589cb commit 8a342be

File tree

2 files changed

+242
-4
lines changed

2 files changed

+242
-4
lines changed

notes/06.变量、存储过程和函数.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ select sum;
189189
3. 存储过程体中的每条sql语句的结尾必须加分号,存储过程的结尾可以使用 delimiter 重新设置
190190
- 语法:`delimiter 结束标记`
191191

192-
```sql
193-
194-
```
195-
196192
#### 2. 调用语法
197193

198194
语法:`call 存储过程名(实参列表);`
@@ -288,6 +284,60 @@ show create procedure myp2;
288284
- 存储过程:可以有0个返回,也可以有多个返回。适合批量插入、批量更新
289285
- 函数:有且仅有1个返回。适合在处理数据后返回一个结果
290286

287+
#### 1. 创建语法
288+
语法:`create function 函数名(参数列表) returns 返回类型 begin 函数体 end`
289+
290+
- 参数列表包含两部分:参数名+参数类型
291+
- 函数体:肯定会有return语句,如果没有会报错
292+
- 函数体仅有一句话,则可以省略begin end
293+
- 使用 delimiter语句 设置结束标记
291294

295+
#### 2. 调用语法
296+
297+
语法:`select 函数名(参数列表)`
292298

299+
- 无参有返回
300+
```sql
301+
#返回公司的员工个数
302+
create funciton myf1() returns int
303+
begin
304+
declare c int default 0;
305+
SELECT
306+
COUNT(*)
307+
INTO c FROM
308+
employees;
309+
return c;
310+
end $
311+
select myf1() $
312+
```
293313

314+
- 有参有返回
315+
```sql
316+
#根据员工名,返回他的工资
317+
create function myf2(empName varchar(20)) returns double
318+
begin
319+
set @sal=0;#定义用户变量
320+
select salary into @sal from employees where last_name=empName;
321+
return @sal;
322+
end $
323+
select myf2('kochhar') $
324+
325+
#根据部门名,返回该部门的平均工资
326+
create function myf3(depName varchar(20)) returns double
327+
begin
328+
declare sal double;
329+
select avg(salary) into sal from employees e join departments d on e.department_id = d.department_id where d.department_name=deptName;
330+
return sal;
331+
end $
332+
select myf3('IT')$
333+
```
334+
335+
#### 3. 查看函数
336+
```sql
337+
show create function myf3;
338+
```
339+
340+
#### 4. 删除函数
341+
```sql
342+
drop function myf3;
343+
```

notes/07.流程控制结构.md

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
## 12. 流程控制结构
2+
3+
- 顺序结构:程序从上往下依次执行
4+
- 分支结构:程序从两条或多条路径中选择一条去执行
5+
- 循环结构:程序在满足一定条件的基础上,重复执行一段代码
6+
7+
### 12.1 分支结构
8+
9+
#### 1. if函数
10+
11+
功能:实现简单的双分支
12+
13+
语法:`select if(表达式1, 表达式2, 表达式3)`
14+
15+
执行顺序:如果表达式1成立,则if函数返回表达式2的值,否则返回表达式3的值
16+
17+
#### 2. case函数
18+
19+
- 情况1:类似于switch语句,一般用于实现的等值判断
20+
- 情况2:类似于多重if语句,一般用于实现区间判断
21+
```sql
22+
#情况1
23+
case 变量|表达式|字段
24+
when 要判断的值 then 返回的值1或语句1;
25+
when 要判断的值2 then 返回的值2或语句2;
26+
...
27+
else 要返回的值n或语句n;
28+
end case;
29+
30+
#情况2
31+
case
32+
when 要判断的条件1 then 返回的值1或语句1;
33+
when 要判断的条件2 then 返回的值2或语句2;
34+
...
35+
else 要返回的值n或语句n;
36+
end case;
37+
```
38+
39+
特点:
40+
- 可以作为表达式,嵌套在其他语句中使用,可以放在任何地方,begin end中或外面
41+
- 可以作为独立的语句去使用,只能放在 begin end中
42+
- 如果when中的值或条件成立,则执行对应的then后面的语句,并且结束case,如果都不满足,则执行else中的语句或值
43+
- else可以省略,如果else省略了,并且所有when条件都不满足,则返回null
44+
45+
```sql
46+
#创建存储过程,根据传入的成绩,显示等级,90-100显示A,80-90显示B,60-80显示C,否则显示D
47+
create procedure test_case(in score int)
48+
begin
49+
case
50+
when socre>=90 and score<=100 then select 'A';
51+
when socre>=80 and score<=90 then select 'B';
52+
when socre>=60 and score<=80 then select 'C';
53+
else select 'D';
54+
end case;
55+
end $
56+
call test_case(95)$
57+
```
58+
59+
#### 3. if结构
60+
61+
功能:实现多重分支
62+
63+
应用场合:应用在begin end中
64+
65+
```sql
66+
if 条件1 then 语句1;
67+
elseif 条件2 then 语句2;
68+
...
69+
else 语句n;
70+
end if;
71+
72+
#根据传入的成绩,返回等级,90-100返回A,80-90返回B,60-80返回C,否则返回D
73+
create function test_if(score int) returns char
74+
begin
75+
if score >= 90 and score <= 100 then return 'A';
76+
elseif score >= 80 then return 'B';
77+
elseif score >= 60 then return 'C';
78+
else return 'D';
79+
end if;
80+
end $
81+
```
82+
83+
### 12.2 循环结构
84+
85+
分类:
86+
- while
87+
- loop
88+
- repeat
89+
90+
循环控制:
91+
- iterate: 类似于 continue
92+
- leave: 类似于 break
93+
94+
#### 1. while
95+
96+
```sql
97+
标签: while 循环条件 do
98+
循环体;
99+
end while 标签;
100+
```
101+
102+
#### 2. loop
103+
- 可以用来模拟简单的死循环
104+
105+
```sql
106+
标签: loop
107+
循环体;
108+
end loop 标签;
109+
```
110+
111+
#### 3. repeat
112+
113+
```sql
114+
标签: repeat
115+
循环体;
116+
until 结束循环的条件
117+
end while 标签;
118+
```
119+
120+
```sql
121+
#1.没有循环控制语句
122+
#批量插入,根据次数插入到admin表中多条记录
123+
create procedure pro_while1(in insertCont int)
124+
begin
125+
declare i int default 1;
126+
while i<=insertCont do
127+
insert into admin(unsername,password) values(concat('rose',i),'666');
128+
set i=i+1;
129+
end while;
130+
end $
131+
call pro_while1(100)$
132+
133+
#2. 添加leave语句
134+
#批量插入,根据次数插入到admin表中多条记录,如果次数>20则停止
135+
truncate table admin$
136+
drop procedure pro_while1$
137+
138+
create procedure pro_while1(in insertCont int)
139+
begin
140+
declare i int default 1;
141+
a:while i<=insertCont do
142+
insert into admin(unsername,password) values(concat('rose',i),'666');
143+
if i>=20 then leave a;
144+
end if;
145+
set i=i+1;
146+
end while a;
147+
end $
148+
call pro_while1(100)$
149+
150+
#3. 添加iterate语句
151+
#批量插入,根据次数插入到admin表中多条记录,只插入偶数次
152+
truncate table admin$
153+
drop procedure pro_while1$
154+
155+
create procedure pro_while1(in insertCont int)
156+
begin
157+
declare i int default 0;
158+
a:while i<=insertCont do
159+
set i=i+1;
160+
if mod(i, 2)!=0 then iterate a;
161+
end if;
162+
insert into admin(unsername,password) values(concat('rose',i),'666');
163+
end while a;
164+
end $
165+
call pro_while1(100)$
166+
```
167+
- 经典案例
168+
```sql
169+
create table stringcontent(
170+
id int primary key auto_increment,
171+
content varchar(20)
172+
);
173+
174+
delimiter $
175+
create procedure test_randstr_insert(in insertCount int)
176+
begin
177+
declare i int default 1;
178+
declare str varchar(26) default 'abcdefghijklmnopqrstuvwxyz';
179+
declare startIndex int default 1;
180+
declare len int default 1;
181+
while i<=insertCont do
182+
set len = floor(rand()*(20-startIndex+1)+1);
183+
set startIndex = floor(rand()*26+1);
184+
insert into stringcontent(content) values(substr(str, startIndex, len));
185+
set i=i+1;
186+
end $
187+
call test_randstr_insert(10)$
188+
```

0 commit comments

Comments
 (0)