当前位置:网站首页>Methods of de duplication and connection query in MySQL database
Methods of de duplication and connection query in MySQL database
2022-07-25 17:33:00 【Yisu cloud】
MySQL Methods of de duplication and connection query in database
This article is for you to know MySQL Methods of de duplication and connection query in database . It has certain reference value , Friends in need can refer to , I hope that's helpful .
Catalog
1. duplicate removal
2. Link query
Use where Perform multi table join query
Internal connection - Equivalent connection
Internal connection - Non equivalent connection
Internal connection - Self join
External connection - Left and right outer connection
Three meter connection
1. duplicate removal
Refer to this article for the contents of the sample table
There are some MySQL There may be duplicate records in the data table , In some cases we allow duplicate data to exist , But sometimes we also need to delete these duplicate data .
for example : To redisplay position information :
mysql> select distinct job from emp;+-----------+| job |+-----------+| CLERK || SALESMAN || MANAGER || ANALYST || PRESIDENT |+-----------+5 rows in set (0.02 sec)
Another example : Combined weightlessness , Find unique information about departments and positions :
mysql> select distinct job,deptno from emp;+-----------+--------+| job | deptno |+-----------+--------+| CLERK | 20 || SALESMAN | 30 || MANAGER | 20 || MANAGER | 30 || MANAGER | 10 || ANALYST | 20 || PRESIDENT | 10 || CLERK | 30 || CLERK | 10 |+-----------+--------+9 rows in set (0.00 sec)
Another example : Now we want to count the number of jobs , Use a combination of count function :
mysql> select count(distinct job) from emp;+---------------------+| count(distinct job) |+---------------------+| 5 |+---------------------+1 row in set (0.00 sec)
2. Link query
We've learned how to read data in a table , It's relatively simple , But in real applications, it is often necessary to read data from multiple data tables .
JOIN According to the function, it can be divided into the following three categories :
INNER JOIN( Internal connection , Or equivalent connection ): Get the records of field matching relationship in two tables .
LEFT JOIN( Left connection ): Get all the records in the left table , Even if the right table does not have a matching record .
RIGHT JOIN( The right connection ): And LEFT JOIN contrary , Used to get all records in the right table , Even if the left table does not have a matching record .
The mechanism of multi table connection is : Take every piece of data from one of the tables , Match data rows from another table . This involves the problem of efficiency control
Use where Perform multi table join query
Now let's demonstrate an example : Take out the name of each employee and department :
mysql> select ename,dname -> from emp,dept -> where emp.deptno = dept.deptno;+--------+------------+| ename | dname |+--------+------------+| SMITH | RESEARCH || ALLEN | SALES || WARD | SALES || JONES | RESEARCH || MARTIN | SALES || BLAKE | SALES || CLARK | ACCOUNTING || SCOTT | RESEARCH || KING | ACCOUNTING || TURNER | SALES || ADAMS | RESEARCH || JAMES | SALES || FORD | RESEARCH || MILLER | ACCOUNTING |+--------+------------+14 rows in set (0.00 sec)
above sql Statements are actually inefficient , We try to optimize ( Alias a table ):(sql92 grammar )
mysql> select e.ename,d.dname -> from emp e,dept d -> where e.deptno = d.deptno;+--------+------------+| ename | dname |+--------+------------+| SMITH | RESEARCH || ALLEN | SALES || WARD | SALES || JONES | RESEARCH || MARTIN | SALES || BLAKE | SALES || CLARK | ACCOUNTING || SCOTT | RESEARCH || KING | ACCOUNTING || TURNER | SALES || ADAMS | RESEARCH || JAMES | SALES || FORD | RESEARCH || MILLER | ACCOUNTING |+--------+------------+14 rows in set (0.00 sec)
Be careful : The more tables are connected , The less efficient , Please try to reduce the connection times of the table !
Internal connection - Equivalent connection
Or the example above , Take out the name of each employee and department :(sql99 grammar )
Internal connection , We use inner
mysql> select e.ename,d.dname -> from emp e -> inner join -> dept d -> on -> e.deptno = d.deptno;+--------+------------+| ename | dname |+--------+------------+| SMITH | RESEARCH || ALLEN | SALES || WARD | SALES || JONES | RESEARCH || MARTIN | SALES || BLAKE | SALES || CLARK | ACCOUNTING || SCOTT | RESEARCH || KING | ACCOUNTING || TURNER | SALES || ADAMS | RESEARCH || JAMES | SALES || FORD | RESEARCH || MILLER | ACCOUNTING |+--------+------------+14 rows in set (0.00 sec)
sql99 The advantages of : The connection of tables is independent , Not occupy where The location of . send sql The whole sentence is clearer
Internal connection - Non equivalent connection
Case study : Find out the salary scale of each employee , Ask to show employee name , Salary , Pay scales
mysql> select -> e.ename,e.sal,s.grade -> from -> emp e -> inner join -> salgrade s -> on -> e.sal between s.losal and s.hisal;+--------+---------+-------+| ename | sal | grade |+--------+---------+-------+| SMITH | 800.00 | 1 || ALLEN | 1600.00 | 3 || WARD | 1250.00 | 2 || JONES | 2975.00 | 4 || MARTIN | 1250.00 | 2 || BLAKE | 2850.00 | 4 || CLARK | 2450.00 | 4 || SCOTT | 3000.00 | 4 || KING | 5000.00 | 5 || TURNER | 1500.00 | 3 || ADAMS | 1100.00 | 1 || JAMES | 950.00 | 1 || FORD | 3000.00 | 4 || MILLER | 1300.00 | 2 |+--------+---------+-------+14 rows in set (0.01 sec)
Internal connection - Self join
Case study : Query the superior of the employee , Employee name and corresponding leader name are required to be displayed
We can find out , The relationship between employees and leaders is in a table , At this point, you need to use self connection ( skill : Look at one table as two tables )
mysql> select -> a.ename as ' Employee name ',b.ename as ' Name of leader ' -> from emp a -> join emp b -> on -> a.mgr = b.empno;+-----------+-----------+| Employee name | Name of leader |+-----------+-----------+| SMITH | FORD || ALLEN | BLAKE || WARD | BLAKE || JONES | KING || MARTIN | BLAKE || BLAKE | KING || CLARK | KING || SCOTT | JONES || TURNER | BLAKE || ADAMS | SCOTT || JAMES | BLAKE || FORD | JONES || MILLER | CLARK |+-----------+-----------+13 rows in set (0.00 sec)
External connection - Left and right outer connection
The difference between external connection and internal connection is , The records of a table whose external connection fails to match will also be taken out
Case study : Find the employee's department information . Ask the Department to find out even if there are no employees
mysql> select -> e.ename,d.dname -> from emp e -> right join dept d -> on -> e.deptno = d.deptno;+--------+------------+| ename | dname |+--------+------------+| SMITH | RESEARCH || ALLEN | SALES || WARD | SALES || JONES | RESEARCH || MARTIN | SALES || BLAKE | SALES || CLARK | ACCOUNTING || SCOTT | RESEARCH || KING | ACCOUNTING || TURNER | SALES || ADAMS | RESEARCH || JAMES | SALES || FORD | RESEARCH || MILLER | ACCOUNTING || NULL | OPERATIONS |+--------+------------+15 rows in set (0.00 sec)
alike , If it's a left outer connection , All data in the left table will be queried , Use left join Key words can be used
The number of external connection query results must be >= Number of query results connected within
Three meter connection
A more complicated situation is , Group table connection
Let's take a case study :
Find out the Department name and salary grade of each employee . Ask to show employee name , Department name , Salary , Pay scales
mysql> select -> e.ename,e.sal,d.dname,s.grade -> from emp e -> join dept d -> on e.deptno = d.deptno -> join salgrade s -> on e.sal between s.losal and s.hisal;+--------+---------+------------+-------+| ename | sal | dname | grade |+--------+---------+------------+-------+| SMITH | 800.00 | RESEARCH | 1 || ALLEN | 1600.00 | SALES | 3 || WARD | 1250.00 | SALES | 2 || JONES | 2975.00 | RESEARCH | 4 || MARTIN | 1250.00 | SALES | 2 || BLAKE | 2850.00 | SALES | 4 || CLARK | 2450.00 | ACCOUNTING | 4 || SCOTT | 3000.00 | RESEARCH | 4 || KING | 5000.00 | ACCOUNTING | 5 || TURNER | 1500.00 | SALES | 3 || ADAMS | 1100.00 | RESEARCH | 1 || JAMES | 950.00 | SALES | 1 || FORD | 3000.00 | RESEARCH | 4 || MILLER | 1300.00 | ACCOUNTING | 2 |+--------+---------+------------+-------+14 rows in set (0.00 sec)
Let's look at a more complicated situation :
Find out the Department name, salary grade and leader name of each employee . Ask to show employee name , Department name , Name of leader , Salary , Pay scales
mysql> select -> e.ename,e.sal,d.dname,s.grade,l.ename -> from emp e -> join dept d -> on e.deptno = d.deptno -> join salgrade s -> on e.sal between s.losal and s.hisal -> left join -> emp l -> on e.mgr = l.empno;+--------+---------+------------+-------+-------+| ename | sal | dname | grade | ename |+--------+---------+------------+-------+-------+| SMITH | 800.00 | RESEARCH | 1 | FORD || ALLEN | 1600.00 | SALES | 3 | BLAKE || WARD | 1250.00 | SALES | 2 | BLAKE || JONES | 2975.00 | RESEARCH | 4 | KING || MARTIN | 1250.00 | SALES | 2 | BLAKE || BLAKE | 2850.00 | SALES | 4 | KING || CLARK | 2450.00 | ACCOUNTING | 4 | KING || SCOTT | 3000.00 | RESEARCH | 4 | JONES || KING | 5000.00 | ACCOUNTING | 5 | NULL || TURNER | 1500.00 | SALES | 3 | BLAKE || ADAMS | 1100.00 | RESEARCH | 1 | SCOTT || JAMES | 950.00 | SALES | 1 | BLAKE || FORD | 3000.00 | RESEARCH | 4 | JONES || MILLER | 1300.00 | ACCOUNTING | 2 | CLARK |+--------+---------+------------+-------+-------+14 rows in set (0.00 sec)
That's all MySQL A brief introduction to the methods of de duplication and connection query in the database , Of course, we have to use the above differences in detail before we can understand . If you want to know more , Welcome to the Yisu cloud industry information channel !
边栏推荐
- Jenkins' file parameters can be used to upload files
- Function name pointer and function pointer
- Redis源码与设计剖析 -- 15.RDB持久化机制
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- new与malloc
- 函数名指针和函数指针
- 理财有保本产品吗?
- [Hardware Engineer] about signal level driving capability
- Virtual memory management
- 栈的顺序存储结构,链式存储结构及实现
猜你喜欢

博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理

Customize MVC project login registration and tree menu

Calculation date or date formatting

POWERBOARD coco! Dino: let target detection embrace transformer

大型仿人机器人的技术难点和应用情况

Chapter III data types and variables

Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation

Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes

【硬件工程师】元器件选型都不会?

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
随机推荐
Hcip notes 11 days
多项式相加
【无标题】
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
Trooper
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Boring post roast about work and life
Is there a principal guaranteed product for financial management?
We were tossed all night by a Kong performance bug
11. Camera and lens
Jenkins' file parameters can be used to upload files
The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
【硬件工程师】元器件选型都不会?
吴恩达机器学习编程作业无法暂停pause问题解决
[cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
Function name pointer and function pointer
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
11、照相机与透镜