当前位置:网站首页>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);
边栏推荐
- [leetcode] sum of two numbers [1]
- 微研所,微生物检验中常用的生化反应
- Construction and beautification of personal blog
- 【队列】933. Number of Recent Calls
- Log logrus third party library usage
- 【动态规划】路径dp:931. Minimum Falling Path Sum
- 【Qt5-基础篇】随机数显示屏展示
- uniapp官方组件点击item无效,解决方案
- MFC TCP communication server client demo notes vs2019
- Basic knowledge 3 - standard unit library
猜你喜欢

物业怎么发短信通知给业主?

Exploration and practice of "flow batch integration" in JD

编译安装oh-my-zsh

元宇宙为 VR/AR 带来的新机会

一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!

Creating ASCII art with C #

neo4j安装、运行以及项目的构建和功能实现

Green, green the reed. dew and frost gleam.

Service grid ASM year end summary: how do end users use the service grid?

Install redis database and download redis Desktop Manager in win11
随机推荐
Document service design
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
Matlab farthest point sampling (FPS improved version)
1175. Prime Arrangements
Using recyclerreview to show banner is very simple
Unknown database connection database error
【Qt5-基础篇】随机数显示屏展示
System.CommandLine版CSRebot
视频教程 | 长安链推出系列视频教程合集(入门)
微生物检测,土壤微生物的作用有哪些?
数字IC设计流程总结
K210 access control complete
Solve idea:class' xxx 'not found in module' xxx‘
基础知识之一——STA基础概述
[leetcode] sum of two numbers [1]
gin_gorm
Kongyiji's first question: how much do you know about service communication?
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
Why build a personal blog