02-017 MySQL_ based flow control structure _

classification

1. sequential structure: program executed from top to bottom
2. The branch structure: Select a program from the two or more paths to carry
3 loop structure: On the basis of satisfying certain conditions, the implementation of a code repeat

Branch structure

1.if Function
Function: double branch simple
syntax: select if (expression 1, expression 2, expression 3);
execution order: 1 if set up, it returns the value 2 or 3 returns
2.case
expression or independently statement (mainly speaks as separate statements)
case 1 : similar equivalent switch generally used for determining
the case variable | expression | field
When judged to be 1 then returns a value of 1 or a value statement 1;
When the determination value to 2 then returns 2 or a value statement 2;
...
the else return to the statement or the value n n;
End case;
case 2 : for generally similar multiple interval determining if
case
When determination conditions to be 1 then returns a value of 1 or statement;
When to 2 or a value judgment condition statement 2 then returns 2;
...
value n or else to return statement n;
End Case;
characteristics:
① can be used as expressions, statements nested within the other, can be placed anywhere, begin end in begin end or outside; you can also use as a stand-alone statement, can be placed only begin end in
② If the value meets or when the condition is true, then the statement following the execution, and the end of the case, if not met, then execute el se statements or values
③else may be omitted, if else is omitted, and all when condition is not satisfied, null is returned
Case : create a stored procedure, based on the incoming results, level display, such as display 90-100 A, 80-90 display B, 60-80 C display or display D

create procedure test_case(in score int)
begin
	case 
	when score >= 90 and score <=100 then select 'A';
	when score >=80 then select 'B';
	when score >=60 then select 'C';
	else select 'D';
	end case;
end $
call test_case(98);

3. Multiple if
syntax:
if condition 1 then statement 1;
ELSEIF condition 2 then statement 2;
...
[else statement n;}
end if;

create function test_if(score int) returns Char
begin
	if score >= 90 and score <=100 then return 'A'; 
	elseif score>=80 then return 'B';
	elseif score>=60 then return 'C';
	else return 'D';
	end if;
end

Loop structure

while, loop, repeat
cycle control:
iterate Similar continue, continue, the end of this cycle, continue to the next
leave similar break, jumped out to end the current cycle where the
syntax:
1.while
[Tags:] conditions do while loop
the loop;
End while [label];
2.loop
[label:] loop
the loop;
end loop [label];
can simulate an infinite loop
3.repeat
[label:] repeat
loop body;
an until the end of the loop condition
end repeat [label];
case: bulk insert, inserted into the admin table according to the number of records in the plurality
no added loop control statements

create procedure pro_while(in insertCount int)
begin 
	declare i int default 1;
	while i<insertCount do
		insert into admin(username,password) values(concat('lily',i),'123456');
		set i=i+1;
	end while;
end $
call test_while(10);

Case: bulk insert, inserted into the plurality of recording admin table according to the number, if the number> 20 stops

create procedure pro_while1(in insertCount int)
begin 
	declare i int default 1;
	a:while i<insertCount do
		insert into admin(username,password) values(concat('hua',i),'123456');
		if i>20 then leave  a;
		set i=i+1;
	end while a;
end $
call test_while1(100);

Add iterate statement

create procedure pro_while2(in insertCount int)
begin 
	declare i int default 0;
	a:while i<insertCount do
		set i=i+1;
		if	mod(i,2)  !0 then iterate a;
		end if;
		insert into admin(username,password) values(concat('huah',i),'123456');
	end while a;
end $
call test_while2(100);

to sum up

Begin end in place

Classic examples

Inserted into the table specified number of random string

#建表
drop table if exists stringcontent;
create table stringcontent)
	id int primary key default 1,
	content varchar(20)
);
#向该表插入指定个数的随机字符串
delimiter $
create procedure test_randstr_insert(in insertCount int)
begin
	declare i int default 1;#定义一个循环变量
	declare str varchar(26) default 'abcdefghijklmnopqrstuvwxyz,;
	declare startIndex int default 1;#起始索引
	declare len int default 1;#字符串截取长度
	while i<insertCount do
		set len = floor(rand()*(20-startIndex+1)+1);
		set startIndex = floor(rand()*26+1);#随机整数
		insert into stringcontent(content) values(substr(str,startIndex,len));
		set i=i+1;#循环变量更新
	end while;
end $

Learn finishing in MySQL Basic + Advanced articles .

Published 53 original articles · won praise 0 · Views 385

Guess you like

Origin blog.csdn.net/weixin_40778497/article/details/103644262