当前位置:网站首页>MySQL functions and related cases and exercises
MySQL functions and related cases and exercises
2022-07-03 13:19:00 【Levi Bebe】
1.MySQL The main function of

1.1 Aggregate functions

group_concat() Use of functions
CREATE DATABASE bianbian4;
USE bianbian4;
CREATE TABLE emp (
emp_id INT PRIMARY KEY auto_increment COMMENT ' Number ',
emp_name VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT ' full name ',
salary DECIMAL ( 10, 2 ) NOT NULL DEFAULT 0 COMMENT ' Wages ',
department VARCHAR ( 20 ) NOT NULL DEFAULT '' COMMENT ' department '
);
INSERT INTO emp ( emp_name, salary, department )
VALUES
( ' Zhang Jingjing ', 5000, ' Finance Department ' ),
( ' Wang Feifei ', 5800, ' Finance Department ' ),
( ' Zhao Gang ', 6200, ' Finance Department ' ),
( ' Liu Xiaobei ', 5700, ' The personnel department ' ),
( ' Wang Dapeng ', 6700, ' The personnel department ' ),
( ' Zhang Xiaopei ', 5200, ' The personnel department ' ),
( ' Liu yunyun ', 7500, ' The sales department ' ),
( ' Liu Yunpeng ', 7200, ' The sales department ' ),
( ' Liu Yunpeng ', 7800, ' The sales department ' );
-- Merge the names of all employees into one line
SELECT GROUP_CONCAT(emp_name) FROM emp;
-- Specify separator merge
SELECT GROUP_CONCAT(emp_name SEPARATOR ';') FROM emp;
-- Specify sorting method and separator
SELECT department,GROUP_CONCAT(emp_name ORDER BY salary desc SEPARATOR ';') FROM emp GROUP BY department;
1.2 Mathematical functions



-- Mathematical functions
SELECT abs(-10);
SELECT abs(10);
SELECT abs( Expression or field ) FROM surface ;
-- ceil Upward evidence collection ;
select ceil(1.1); -- 2
select ceil(1.0); -- 1
-- Rounding down
select floor(1.1); -- 1
select floor(1.9); -- 1
-- Taking the maximum
select greatest(1,2); -- 2
-- minimum value
select least(1,2,3); -- 1
1.3 String function




1.4 Date function







1.5 Control flow function
1.5.1 if Logical judgment statement
When judging the status , have access to IF Function to judge :
1.5.2 case when function
case when amount to C Linguistic switch() sentence 
1.6 Window function

Window function classification :
Window function syntax structure :
1.6.1 Ordinal function
Ordinal function :row_number()、rank()、dense_rank(), Can be used to achieve grouping sorting , And add serial number .
use bianbian4;
create table employee(
dname varchar(20),
eid varchar(20),
ename varchar(20),
hiredate date,
salary double
);
-- The data class contains
insert into
employee
values
(' R & D department ','1001',' Liu bei ','2021-11-01',3000),
(' R & D department ','1002',' Guan yu ','2021-11-02',5000),
(' R & D department ','1003',' Zhang Fei ','2021-11-03',7000),
(' R & D department ','1004',' zhaoyun ','2021-11-04',7000),
(' R & D department ','1005',' d ','2021-11-05',4000),
(' R & D department ','1006',' Huang Zhong ','2021-11-06',4000),
(' The sales department ','1001',' Cao Cao ','2021-11-01',2000),
(' The sales department ','1002',' Xu Chu ','2021-11-02',3000),
(' The sales department ','1003',' Dianwei ','2021-11-03',5000),
(' The sales department ','1004',' Zhang liao ','2021-11-04',6000),
(' The sales department ','1005',' Xu Huang ','2021-11-05',9000),
(' The sales department ','1006',' Cao Hong ','2021-11-06',6000);
-- Sort the employees of each department according to their salary , And give the ranking
select dname,ename,salary,
row_number() over (partition by dname order by salary desc) as row_rank,
rank() over (partition by dname order by salary desc) as rk,
dense_rank() over (partition by dname order by salary desc) as dense_rk
from employee;
-- Find out the top three employees in each department ,- Grouping calculation topN
select *
from
(
select dname,ename,salary,
dense_rank() over (partition by dname order by salary desc) as dense_rk
from employee
) as t
where t.dense_rk<=3;
-- Sort all employees globally
select dname,ename,salary,
dense_rank() over (order by salary desc) as dense_rk
from employee
row_number(): Don't consider the differences , The numbers in the order do not change ,1,2,3
rank(): Consider the same situation , Skip number
dense_rank(): Consider the same situation , Don't jump numbers
Here's the picture :
1.6.2 Windowed aggregate function

-- Accumulate according to the grouping date
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate ) as c1
from employee

-- without order by Add all the data in the Group
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname ) as c1
from employee

Realize interval statistics , With sum As an example , The data is the data created above :
-- Specify the range to add
-- Start line : unbounded preceding To the current line current row
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between unbounded preceding and current row) as c1
from employee
-- The top three lines of the current line
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between 3 preceding and current row) as c1
from employee
-- Three lines up + Current row + Next line
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between 3 preceding and 1 following) as c1
from employee
-- Current line to the end
select
dname,
ename,
hiredate,
salary,
sum(salary) over (partition by dname order by hiredate rows between current row and unbounded following) as c1
from employee
1.6.3 Distribution function
cume_dist() Introduction to :
purpose : Less than... In the group 、 Equal to current rank The number of rows for the value / The total number of rows in the group
Application scenarios : Query the comparison column less than or equal to the current salary 
cume_dist() Example :
use bianbian4;
select
dname,
ename,
salary,
cume_dist() over(order by salary) as rn1,
cume_dist() over(partition by dname order by salary) as rn2
from employee;
/* No addition partition by Is the total number of lines : rn1 It indicates the proportion of the whole salary less than or equal to the current number 3/12 = 0.25 (3000) 5/12 = 0.4166666666666667 (4000) Yes partition by, Look at this group : rn2 Meaning case : 1 / 6 = 0.16666666666666666 (3000, In the R & D group ) */
cume_dist() Result :
percent_rank Introduction to :
percent_rank Code :
select
dname,
ename,
salary,
rank() over(partition by dname order by salary desc) as rn1,
percent_rank() over(partition by dname order by salary desc) as rn2
from employee;
/* rn2: first line :(1-1)/ (6-1) = 0 The second line :(1-1)/ (6-1) = 0 The third line :(3-1)/ (6-1) = 0.4 */
percent_rank() result :
1.6.4 Before and after function LAG and LEAD

select
dname,
ename,
salary,
hiredate,
-- Put the... In the previous line , Put it behind the current , If there is no default '2000-01-01'
lag(hiredate,1,'2000-01-01') over(partition by dname order by hiredate ) as rn1,
-- Put the values of the last two lines , Put it behind the current , If there is no default null
lag(hiredate,2) over(partition by dname order by hiredate ) as rn2
from employee;

1.6.5 Head tail function first_value and last_value

first_value() and last_value() Case study :
select
dname,
ename,
salary,
hiredate,
-- The first one so far
first_value(salary) over(partition by dname order by hiredate ) as first_,
-- The last one so far
last_value(salary) over(partition by dname order by hiredate ) as last_
from employee;

1.6.6 Other functions nth_value(expr,n)、ntile(n)

nth_value(expr,n) Case study :
select
dname,
ename,
salary,
hiredate,
-- No. so far 1 Salary of the line
nth_value(salary,1) over(partition by dname order by hiredate ) as first_salary,
-- No. so far 2 Salary of the line
nth_value(salary,2) over(partition by dname order by hiredate ) as second_salary
from employee;


ntile Case study :
select
dname,
ename,
salary,
hiredate,
-- Divide into 3 Group
ntile(3) over(partition by dname order by hiredate ) as nt
from employee;
-- Take out the first group of employees in each department
select *
from
(
select
dname,
ename,
salary,
hiredate,
-- Divide into 3 Group
ntile(3) over(partition by dname order by hiredate ) as nt
from employee
) t
where t.nt =1;

Reference resources
https://www.bilibili.com/video/BV1iF411z7Pu?p=99&spm_id_from=pageDriver
边栏推荐
- CVPR 2022 image restoration paper
- SSH login server sends a reminder
- Seven habits of highly effective people
- Some thoughts on business
- 剑指 Offer 15. 二进制中1的个数
- Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
- Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
- Finite State Machine FSM
- C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
- Understanding of CPU buffer line
猜你喜欢

已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)

Road construction issues

剑指 Offer 12. 矩阵中的路径

Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?

我的创作纪念日:五周年

今日睡眠质量记录77分

My creation anniversary: the fifth anniversary

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

MySQL constraints

Flink SQL knows why (12): is it difficult to join streams? (top)
随机推荐
用户和组命令练习
有限状态机FSM
JSP and filter
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
2022-02-14 incluxdb cluster write data writetoshard parsing
Smbms project
父亲和篮球
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
CVPR 2022 image restoration paper
剑指 Offer 17. 打印从1到最大的n位数
A large select drop-down box, village in Chaoyang District
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
STM32 and motor development (from MCU to architecture design)
Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
Task6: using transformer for emotion analysis
When we are doing flow batch integration, what are we doing?
Image component in ETS development mode of openharmony application development
[colab] [7 methods of using external data]
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
已解决(机器学习中查看数据信息报错)AttributeError: target_names