当前位置:网站首页>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 !
边栏推荐
- 02.两数相加
- pgsql有没有好用的图形化管理工具?
- Using rank to discuss the solution of linear equations / the positional relationship of three planes
- 01.两数之和
- 网上开期货账户安全吗?手续费怎么申请才低?
- PostgreSQL passwords are case sensitive. Is there parameter control?
- 电子产品EMC不合格,如何整改?
- Replicate swin on Huawei ascend910_ transformer
- Add batch delete
- stm32F407------SPI
猜你喜欢

Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?

Multi tenant software development architecture

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

Ultimate doll 2.0 | cloud native delivery package

EDI 对接CommerceHub OrderStream

如何看一本书

OSPF综合实验

虚拟内存管理

世界各地的标志性建筑物

Add batch delete
随机推荐
I'm also drunk. Eureka delayed registration and this pit!
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
EasyUI drop-down box, add and put on and off shelves of products
博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Solution for win10 device management not recognizing gtx1080ti display device
Update 3dcat real time cloud rendering V2.1.2 release
Summary of knowledge points for final review of server-side architecture design
[vscode] support argparser/ accept command line parameters
Lvgl 7.11 tileview interface cycle switching
一篇文章了解超声波加湿器
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
[PHP pseudo protocol] source code reading, file reading and writing, and arbitrary PHP command execution
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
02. Add two numbers
EasyUI modification and DataGrid dialog form control use
How to rectify the unqualified EMC of electronic products?
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
做智能硬件要考虑的产品生命周期
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?