当前位置:网站首页>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);
边栏推荐
- TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
- 6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
- 孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
- Basic knowledge of software and hardware -- diary (1)
- 视频教程 | 长安链推出系列视频教程合集(入门)
- Install redis database and download redis Desktop Manager in win11
- Digital IC design process summary
- 关于白盒测试,这些技巧你得游刃有余~
- 面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
- Exploration and practice of "flow batch integration" in JD
猜你喜欢

Basic knowledge II - Basic definitions related to sta

亲测有效,快速创建JMeter桌面快捷方式

The argument type 'function' can't be assigned to the parameter type 'void function()‘
![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]

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

医疗HIS行业短信发送解决方案

图的连通性基础

Digital IC design process summary

Using recyclerreview to show banner is very simple

Call the classic architecture and build the model based on the classic
随机推荐
图的连通性基础
MATLAB 最远点采样(FPS改进版)
日志 logrus第三方库的使用
孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
Construction and beautification of personal blog
那些一门心思研究自动化测试的人,后来怎样了?
做生意更加务实
[simulation] 922 Sort Array By Parity II
尝试新的可能
User defined annotation implementation verification
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
【Proteus仿真】Arduino UNO +74C922键盘解码驱动4X4矩阵键盘
Green, green the reed. dew and frost gleam.
Basic knowledge of software and hardware -- diary (1)
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
【Qt5-基础篇】随机数显示屏展示
迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况
流批一体在京东的探索与实践
Uniapp official component clicking item is invalid, solution
Sun Yuchen told Swiss media Bilan that the bear market will not last long