当前位置:网站首页>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);
边栏推荐
- 基础知识之一——STA基础概述
- visual studio 2019 下载
- Service grid ASM year end summary: how do end users use the service grid?
- What will Web3 bring in the future?
- 一些本质的区别
- MFC TCP communication server client demo notes vs2019
- Solve idea:class' xxx 'not found in module' xxx‘
- 图灵奖得主LeCun指明AI未来的出路在于自主学习,这家公司已踏上征途
- Relationship between ASCII, Unicode, GBK, UTF-8
- K210 site helmet
猜你喜欢
Docker deployment MySQL 8
Green, green the reed. dew and frost gleam.
视频教程 | 长安链推出系列视频教程合集(入门)
Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
45岁程序员告诉你:程序员为什么要跳槽,太真实...
Introduction and principle analysis of cluster and LVS
微生物健康,食品微生物检测为什么很重要
Microbiological health, why is food microbiological testing important
Gin configuration file
微研所,微生物检验中常用的生化反应
随机推荐
系统设置大页
Creating ASCII art with C #
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况
[simulation] 922 Sort Array By Parity II
那些一门心思研究自动化测试的人,后来怎样了?
45岁程序员告诉你:程序员为什么要跳槽,太真实...
Basic knowledge II - Basic definitions related to sta
[dynamic planning] path dp:931 Minimum Falling Path Sum
Why not two or four TCP handshakes
图的连通性基础
[leetcode] climb stairs [70]
Parity linked list [two general directions of linked list operation]
Zero of DC learning notes -- overview and basic process introduction
关于白盒测试,这些技巧你得游刃有余~
使用StrictMode-StrictMode原理(1)
Using recyclerreview to show banner is very simple
Strictmode jamming and leakage detection -strictmode principle (2)
Open3d point cloud color rendering
Why build a personal blog