当前位置:网站首页>Mysql database foundation: process control
Mysql database foundation: process control
2022-07-01 01:33:00 【Long lasting bangbangjun】
Process control
1、 grammar
1.1 Sequential structure
Too simple , Too lazy to write.
1.2 Branching structure
# ==========================================
# if function :
# grammar :if( expression 1, expression 2, expression 3)
# explain : If the expression 1 establish , So execute the expression 2, Otherwise, execute the expression 3
# If the comparison result is true, select the left , Select the right side for the false
SELECT IF(10<5,' Big ',' Small '); # Small
# for example :
SELECT last_name,commission_pct,IF(commission_pct IS NULL,' No bonus , ha-ha ',' Bonus , Joy ') remarks FROM employees;
#========================================
# Multiple branches
# if...else... function
# grammar :
/* if Conditions 1 then sentence 1; elseif Conditions 2 then sentence 2; ... 【else sentence n;】 end if; */
# applications : Apply to begin end in
# ======================================
# case function
# Use 1: case Be similar to switch The effect of
/* case The field or expression to determine when Constant 1 then The value to display 1【 Or words 1;】 when Constant 2 then The value to display 2【 Or words 2;】 ... else The value to display n【 Or words n;】 end【case;】 AS name FROM Table name ; */
# for example :
/* Case study : Check the salary of employees , The requirements are as follows : Department number =30, The wage shown is 1.1 times Department number =30, The wage shown is 1.2 times Department number =40, The wage shown is 1.3 times Other departments , The wage shown is the original wage */
# Realization :
SELECT
salary The original wage ,department_id,
CASE
department_id
WHEN 30 THEN salary*1.1
WHEN 40 THEN salary*1.2
WHEN 50 THEN salary*1.3
ELSE
salary
END AS New pay
FROM
employees;
# The new salary is listed at the end of the queried table
# Use 2: Similar to multiple if
/* case when Conditions 1 then The value to display 1【 Or words 1;】 when Conditions 2 then The value to display 2【 Or words 2;】 ... else The value to display n【 Or words n;】 end 【case;】 */
# for example :
/* Case study : Check the salary of employees If wages >20000, Show A Level If wages >15000, Show B Level If wages >10000, Show C Level Otherwise, it will show D Level */
# Realization
SELECT
salary Wages ,
CASE
WHEN salary>20000 THEN 'A'
WHEN salary>15000 THEN 'B'
WHEN salary>10000 THEN 'C'
ELSE
'D'
END AS Salary level
FROM
employees;
It should be noted that ,case In structure , If then perhaps else Followed by ordinary values , There is no need to add a semicolon , If the sentence is followed , that then You need a semicolon after it ,end We need to add 【case;】
- Loop structure
classification :while、loop、repeat
Cycle control :
(1)iterate, Be similar to java in continue, It means the end of this cycle , So let's go to the next loop ,iterate Followed by a circular label
(2)leave, Be similar to java Medium break, Indicates the end of the current loop ,leave Followed by a circular label
Be careful : Loop structures need to be placed in begin end In the sentence ( Stored procedure or function begin end in )
# ====================================
# while
# grammar :
/* 【 label :】 while The loop condition do The loop body ; end while【 label 】; */
# ====================================
# repeat, Be similar to do while
# grammar
/* 【 label :】 repeat The loop body ; until Conditions for ending the cycle end repeat 【 label 】; */
# ====================================
# loop, An unconditional loop
# grammar
/* 【 label :】 loop The loop body ; end loop【 label 】 It can be used to simulate a simple dead cycle */
2、 exercises
# ===============================================
# establish admin surface
CREATE TABLE admin1(
id INT PRIMARY KEY,
username VARCHAR(10) NOT NULL,
password INT NOT NULL
);
select * from admin1;
# -----------------------------------------------
# exercises 1: Go to admin Insert in table 100 Data ( Loop structure implementation )
# Set the terminator to $$
delimiter $$
CREATE PROCEDURE pro_while(IN insetCount INT)
BEGIN
# Declare the variable to be given
DECLARE i INT DEFAULT 1;
WHILE i<=insetCount DO
INSERT INTO admin1(id,username,password) values (i,CONCAT('rose',i),666);
SET i = i+1;
END WHILE;
END
$$
# Reset the end character to ;
delimiter ;
# Execute stored procedures
CALL pro_while(100);
# Delete stored procedure
DROP PROCEDURE pro_while;
# -----------------------------------------
# exercises 2: Delete admin1 In the table id Even data
delimiter $$
CREATE PROCEDURE pro_while_delete()
BEGIN
DECLARE i INT DEFAULT 0;
# Assign the query result to @numbers User variables
SELECT count(*) FROM admin1 INTO @numbers;
sign:WHILE i<@numbers DO
SET i=i+1;
# If it's not even , So use iterate End this cycle , Into the next loop
IF MOD(i,2)!=0 THEN ITERATE sign;
END IF;
# Delete id=i( even numbers ) The data of
DELETE FROM admin1 WHERE id=i;
END WHILE sign;
END
$$
delimiter ;
CALL pro_while_delete();
DROP PROCEDURE pro_while_delete;
#------------------------------------------
# exercises 3: Inserts a specified number of random strings into the specified table
# Title Description
/* Table name :stringcontent Which field id: Self growth content varchar(20) requirement : Insert a specified number of random strings into the table */
# Create table stringcontent
CREATE TABLE stringcontent(
id INT PRIMARY KEY AUTO_INCREMENT,
content VARCHAR(20)
);
DROP TABLE IF EXISTS stringcontent;
delimiter $$
CREATE PROCEDURE test_randstr_insert(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1; # Control cycle
DECLARE str VARCHAR(26) DEFAULT 'abcdefghijklmnopqrstuvwxyz'; # 26 A string of letters
DECLARE startIndex INT DEFAULT 1; # Starting index
DECLARE len INT DEFAULT 1; # The length of the intercepted character
WHILE i<=insertCount DO
# Generate a random integer , Represents the starting index of the string 1~26
SET startIndex = FLOOR(RAND()*26+1);
# Generate a random integer , Used to represent the intercept length
SET len = FLOOR(RAND()*(26-startIndex+1)+1);
SET i = i+1;
# Insert the intercepted string into the table
INSERT INTO stringcontent(content) values(SUBSTR(str,startIndex,len));
END WHILE;
END
$$
delimiter ;
# Calling stored procedure
CALL test_randstr_insert(10);
边栏推荐
- Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
- 面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
- Using recyclerreview to show banner is very simple
- Lecun, a Turing Award winner, pointed out that the future of AI lies in self-learning, and the company has embarked on the journey
- Unknown database connection database error
- Green, green the reed. dew and frost gleam.
- Construction and beautification of personal blog
- 【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
- 【动态规划】路径dp:931. Minimum Falling Path Sum
- 为什么要搭建个人博客
猜你喜欢

K210 site helmet

Digital IC design process summary

孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久

图的连通性基础

New opportunities for vr/ar brought by metauniverse

Unknown database connection database error

Gin configuration file

"Open math input panel" in MathType editing in win11 is gray and cannot be edited

dc_labs--lab1的学习与总结
![Split the linked list [take next first and then cut the linked list to prevent chain breakage]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
随机推荐
C# 自定义并动态切换光标
工作6年,来盘点一下职场人混迹职场的黄金法则
6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
gin_gorm
MFC TCP communication server client demo notes vs2019
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
System settings large page
Pre training / transfer learning of models
StrictMode卡顿与泄漏检测-StrictMode原理(2)
Construction and beautification of personal blog
JS方法大全的一个小文档
用 Flutter 的 Canvas 画点有趣的图形
[stack] 921 Minimum Add to Make Parentheses Valid
Q play soft large toast to bring more comfortable sleep
MFC TCP通信服务端客户端Demo备忘vs2019
OCR的一些项目
做生意更加务实
Basic knowledge 3 - standard unit library