Process sequence structure:
Sequence structure: the program is executed sequentially from top to bottom
Branch structure: the program selects one of two or more paths to execute
Loop structure: the program repeatedly executes a piece of code on the basis of meeting certain conditions
The sequence structure is relatively simple, no more explanation, let’s take a look at the branch structure and the loop structure.
1. Branch structure:
1. If function
Function: realize simple double branch
grammar:
if (expression 1, expression 2, expression 3)
Order of execution:
If expression 1 is true, the if function returns the value of expression 2, otherwise it returns the value of expression 3
Application: anywhere
2. Case structure
Case 1: Similar to the switch statement in java, generally used to achieve equivalence judgment
grammar:
case variable|expression|field
when the value to be judged then return value 1 or statement 1;
when the value to be judged then return value 2 or statement 2;
.....
else the value or statement to be returned n;
end case;
Case 2: Similar to multiple if statements in java, generally used to realize interval judgment
case
When condition to be judged 1 then return value 1 or statement 1;
When condition to be judged 2 then return value 2 or statement 2;
.....
else the value or statement to be returned n;
end case;
Features:
Can be used as an expression, nested in other statements, can be placed anywhere, in the begin end or outside the begin end
Can be used as an independent statement, can only be placed in the begin end
If the value in when is satisfied or the condition is established, execute the statement following the corresponding then and end the case
If not satisfied, execute the statement or value in else
The else statement can be omitted, if the else is omitted, and all the where conditions are not met, null is returned
Example:
#创建一个存储过程,根据传入的成绩,来显示等级
create procedure test_case(in score int)
begin
case
when score >=90 then select 'A';
when score >=80 then select 'B';
when score >=60 then select 'C';
else select 'D';
end case;
end $
3. If structure
Function: Realize multiple branches
grammar:
if condition 1 then statement 1
elseif condition 2 then statement 2
else statement n
end if;
Application occasions: can only be used in begin end
This is more common and simple, so I won’t give an example
Second, the loop structure
classification:
while 、loop 、repeat
Loop control (jump statement):
iterate is similar to continue to end this cycle and continue to the next
leave is similar to break and ends the loop directly
1、while
grammar:
[Label:] while loop condition do
Loop body
end while [label];
Example
#批量插入,根据次数插入到admin表中多条记录
create procedure pro_while(in insertCount int)
begin
declare i int default 1;
while i< = insertCount do
insert into admin(username ,password) values(concat('rose',i),'666');
set i=i+1;
end while;
end $
#加上跳出语句,注意区分两者的区别(要加标签)
create procedure pro_while(in insertCount int)
begin
declare i int default 1;
a:while i< = insertCount do
insert into admin(username ,password) values(concat('rose',i),'666');
if i>=20 then leave a;
end if;
set i=i+1;
end while a;
end $
#跳出语句是iterate
create procedure pro_while(in insertCount int)
begin
declare i int default 1;
a:while i< = insertCount do
set i=i+1;
if mod(i,2)!=0 then iterate
end if
insert into admin(username ,password) values(concat('rose',i),'666');
end while a;
end $
2、loop
grammar:
【Tag:】loop
Loop body
end loop [label];
Can be used to simulate a simple endless loop
3、repeat
grammar:
【Tag:】repeat
Loop body
until condition to end the loop
end repeat [label];