Mysql based learning (xiv) --- flow control structures

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_40990836/article/details/101541934

Flow control structures

Sequential structure: program executed sequentially downward from the
branch structure: program selection from two or a plurality of paths to perform the
loop structure: the program on the basis of certain conditions, implementation of a code repeat

Branch structure
  1. IF function
    function: to achieve a simple two-branch
    syntax:
SELECT IF(表达式1, 表达式2, 表达式3)

The order of execution:
if 表达式1true, the IFfunction returns 表达式2a value, otherwise the return 表达式3value of
applications: anywhere

CASE structure

Case 1: similar to the switch statement in java, generally used to achieve the equivalent of judging
grammatical

CASE 变量|表达式 | 字段
WHEN 要判断的值 THEN 返回的值1
WHEN 要判断的值 THEN 返回的值2
...
ELSE 要返回的值n
END

Case 2: Multiple IF statement similar java in the determination interval is generally used to implement the
grammar

CASE 
WHEN 要判断的条件1 THEN 返回的值1或语句
WHEN 要判断的条件2 THEN 返回的值2或语句
...
ELSE 要返回的值 n
END

Features:

  1. Can be used as expressions, statements nested in the other, can be placed anywhere in or outside the END BEGIN BEGIN command. You can go as a standalone statement can only be placed in a BEGIN END
  2. If the THENvalue is satisfied or the condition is satisfied, executing the corresponding THENback of the statement and ends CASE, if not met. Execute ELSEstatement or values
  3. ELSEMay be omitted, if ELSEomitted, and all of the WHENconditions are not satisfied, returnNULL

Case 1: Create a stored procedure, according to the results passed to the display level, such as incoming results, 90 - 100: A, 80 - 90: B,
60 - 80: C, Other: D

CREATE PROCEDURE test_case(IN score INT)
BEGIN
	CASE 
	WHEN score >= 90 AND score <= 100 THEN SELECT 'A';
	WHEN socre >= 80 THEN SELECT 'B';
	WHEN score >= 60 THEN SELECT 'C';
	ELSE SELECT 'D';
	END CASE;
END $
# 调用函数
CALL test_case(60)$
# 返回结果
+---+
| C |
+---+
| C |
+---+

Case 2: IF structure to achieve multiple branches incoming results, 90 - 100: A, 80 - 90: B, 60 - 80: C, Other: D
Syntax

if 条件1 then 语句1;
else if 条件2 then 语句2;
...
[else 语句n;]
end if;

Begin end can only be applied in

CREATE FUNCTION test_if(score INT) RETURNS char 
BEGIN 
	IF score >=90 AND score <=100 THEN RETURN 'A'; 
	ELSE IF score >= 80 THEN RETURN 'B'; 
	ELSE IF score >= 60 THEN RETURN 'C'; 
	ELSE RETURN 'D'; 
	END IF; 
END $ 
Loop structure

Category: while, loop, repeat
cycle control: iterateSimilar to continuecontinue, the end of this cycle, continue to the next
leavesimilar break; out of circulation, the end of the current location

Syntax while
features:先判断后执行

标记:while 循环条件 do
	循环体
end while 标签;

Syntax loop
Features:没有条件的死循环

标记: loop 
	循环体
end loop 标签

Syntax repeat
Features:先执行后判断

标记:repeat
	循环体
until 结束循环的条件
end repeat 标记;

Case 1: Batch inserted, is inserted into the plurality amdin table according to the number of records

DELIMITER $
mysql> CREATE PROCEDURE pro_while(IN insertCount INT)
    -> BEGIN
    -> DECLARE i int DEFAULT 1;
    -> WHILE i <= insertCount DO
    -> INSERT INTO admin(name,`password`) VALUES ('gss'+i,'6666');
    -> SET i = i + 1;
    -> END WHILE;
    -> END $
# 调用存储过程
CALL pro_while(100);
# 插入结果 。没有放出全部的
+-----+------+----------+
| id  | name | password |
+-----+------+----------+
|   1 | gss  | 6666     |
|   2 | gss  | 6666     |
|   3 | gss  | 6666     |
|   4 | gss  | 6666     |
|	100| gss | 6666 	|
+-----+------+----------+

Case 2: bulk insert, inserted into the admin table according to the number of records in plurality, greater than 20 times the number of stops

# 清空刚刚的所有数据
TRUNCATE  TABLE admin$
# 创建存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `test_while`(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
a: WHILE i <= insertCount DO
INSERT INTO admin(`name`,`password`) VALUES ('gss','111');
IF i >=20 THEN LEAVE a;
END IF;
SET i = i+1;
END WHILE a;
END
# 调用存储过程
CALL test_while(100)$
# 查看结果
select * from admin$

+----+------+----------+
| id | name | password |
+----+------+----------+
|  1 | gss  | 111      |
|  2 | gss  | 111      |
|  3 | gss  | 111      |
|  4 | gss  | 111      |
|  5 | gss  | 111      |
|  6 | gss  | 111      |
|  7 | gss  | 111      |
|  8 | gss  | 111      |
|  9 | gss  | 111      |
| 10 | gss  | 111      |
| 11 | gss  | 111      |
| 12 | gss  | 111      |
| 13 | gss  | 111      |
| 14 | gss  | 111      |
| 15 | gss  | 111      |
| 16 | gss  | 111      |
| 17 | gss  | 111      |
| 18 | gss  | 111      |
| 19 | gss  | 111      |
| 20 | gss  | 111      |
+----+------+----------+

Guess you like

Origin blog.csdn.net/qq_40990836/article/details/101541934