|
| 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