Flow control structure #
/ *
sequence, branched, cyclic
*/
# A branched structure
# 1.If function
/ *
Syntax: if (condition value 1, value 2)
Function: dual branch
application or outside of the begin end
*/
# 2.case structure
/ *
Syntax:
Case 1: In analogy Switch
Case variable or expression
when value 1 then statement 1;
when value 2 then statement 2;
...
the else statement n-;
End
Case 2:
Case
When condition 1 then statement. 1;
When condition 2 then statement 2;
...
the else statement n-;
End
Application begin end or outside
*/
Structure # 3.if
/ *
Syntax is:
if condition 1 then statement 1;
ELSEIF condition 2 then statement 2;
....
the else statement to the n-;
End if;
function: if multiple similar
Begin end can only be applied in
*/
Case # 1: Create function to achieve passing grades, if the score> 90, returns to A, if the score> 80, returns to B, if the score> 60, returns C, otherwise D
CREATE FUNCTION test_if(score FLOAT) RETURNS CHAR
BEGIN
DECLARE ch CHAR DEFAULT 'A';
IF score>90 THEN SET ch='A';
ELSEIF score>80 THEN SET ch='B';
ELSEIF score>60 THEN SET ch='C';
ELSE SET ch='D';
END IF;
RETURN ch;
END $
SELECT test_if(87)$
Case # 2: Create a stored procedure, if the salary <2000, then deleted, if 5000> wage> 2000, the 1000 pay rise, otherwise raise 500
CREATE PROCEDURE test_if_pro(IN sal DOUBLE)
BEGIN
IF sal<2000 THEN DELETE FROM employees WHERE employees.salary=sal;
ELSEIF sal>=2000 AND sal<5000 THEN UPDATE employees SET salary=salary+1000 WHERE employees.`salary`=sal;
ELSE UPDATE employees SET salary=salary+500 WHERE employees.`salary`=sal;
END IF;
END $
CALL test_if_pro(2100)$
Case # 1: Create function to achieve passing grades, if the score> 90, returns to A, if the score> 80, returns to B, if the score> 60, returns C, otherwise D
CREATE FUNCTION test_case(score FLOAT) RETURNS CHAR
BEGIN
DECLARE ch CHAR DEFAULT 'A';
CASE
WHEN score>90 THEN SET ch='A';
WHEN score>80 THEN SET ch='B';
WHEN score>60 THEN SET ch='C';
ELSE SET ch='D';
END CASE;
RETURN ch;
END $
SELECT test_case(56)$
# Second, the loop structure
/ *
classification:
the while, Loop, REPEAT
Loop control:
iterate similar to continue, continue, the end of this cycle, continue to the next
leave similar break, jump, where the end of the loop current
*/
#1.while
/*
grammar:
[Label:] conditions do while loop
loop;
End tag [while];
Lenovo:
while (loop condition) {
Loop body;
}
*/
#2.loop
/*
Syntax:
[label:] loop
the loop;
End loop [label];
It can be used to simulate a simple infinite loop
*/
3.repeat #
/ *
Syntax:
[label:] repeat
loop body;
an until the end of the loop condition
end repeat [label];
*/
# 1 no added loop control statements
# Case: bulk insert, according to the number of inserted admin table plurality of recording
the DROP PROCEDURE pro_while1 $
the CREATE PROCEDURE pro_while1 (the IN insertCount the INT)
the BEGIN
the DECLARE I the INT the DEFAULT 1;
the WHILE I <= insertCount the DO
the INSERT ADMIN the INTO (username, `password`) the VALUES (CONCAT ( 'Rose', I), '666');
the SET I = I +. 1;
the END the WHILE;
the END $
CALL pro_while1(100)$
/*
int i=1;
while(i<=insertcount){
// Insert
i ++;
}
*/
# 2. Adding leave statement
# Case: bulk insert, inserted into the admin table multiple records depending on the number, if the number> 20 stops
TRUNCATE TABLE admin $
the DROP PROCEDURE test_while1 $
the CREATE PROCEDURE test_while1 (the IN insertCount the INT)
the BEGIN
the DECLARE I the INT the DEFAULT. 1;
A: the WHILE I <= the DO insertCount
the INSERT the INTO ADMIN (username, `password`) the VALUES (CONCAT ( 'xiaohua', I), '0000');
the IF I> = 20 is the LEAVE THEN A;
the END the IF;
the SET I = I +. 1;
the END A the WHILE;
the END $
CALL test_while1(100)$
# 3. Add iterate statement
# Case: bulk insert, inserted into the admin table plurality of records based on the number of inserted only even-numbered
TRUNCATE TABLE admin $
the DROP PROCEDURE test_while1 $
the CREATE PROCEDURE test_while1 (the IN insertCount the INT)
the BEGIN
the DECLARE I the INT the DEFAULT 0;
A: the WHILE I <= the DO insertCount
the SET I = I +. 1;
! the IF the MOD (I, 2) = 0 THEN the ITERATE A;
the END the IF;
the INSERT the INTO ADMIN (username, `password`) the VALUES (CONCAT ( 'xiaohua', I), '0000' );
the END the WHILE A;
the END $
CALL test_while1(100)$
/*
int i=0;
while(i<=insertCount){
i++;
if(i%2==0){
continue;
}
插入
}
*/