当前位置:网站首页>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 !
边栏推荐
- Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
- Gtx1080ti fiber HDMI interference flash screen 1080ti flash screen solution
- 无聊发文吐槽工作生活
- [knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
- MySQL数据库中去重与连接查询的方法
- Random talk on generation diffusion model: DDPM = Bayesian + denoising
- 04. Find the median of two positive arrays
- [Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
- How to fix the first row title when scrolling down in Excel table / WPS table?
- 咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
猜你喜欢

一篇文章了解超声波加湿器

How to delete Microsoft Pinyin input method in win10

How to fix the first row title when scrolling down in Excel table / WPS table?

哈夫曼树的构建

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

Using rank to discuss the solution of linear equations / the positional relationship of three planes

【解决方案】Microsoft Edge 浏览器 出现“无法访问该页面”问题
![Sogou batch push software - Sogou batch push tool [2022 latest]](/img/87/d89c8d301743d1087d001a4f97de02.jpg)
Sogou batch push software - Sogou batch push tool [2022 latest]

四六级

对灰度图像的三维函数显示
随机推荐
What is metauniverse gamefi chain game system development? Development and application case and analysis of gamefi metauniverse NFT chain game system
四六级
The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
Technical difficulties and applications of large humanoid robots
How to prevent the unburned gas when the city gas safety is alarmed again?
Redis cluster deployment based on redis6.2.4
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
Outlook tutorial, how to search for calendar items in outlook?
【硬件工程师】关于信号电平驱动能力
【硬件工程师】DC-DC隔离式开关电源模块为什么会用到变压器?
Cet
新版selenium4.3在egde浏览器的无头模式
无聊发文吐槽工作生活
Trooper
电子产品EMC不合格,如何整改?
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
约瑟夫环问题
【VSCODE】支持argparser/接受命令行参数
Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
电子产品“使用”和“放置”哪个寿命更长??