当前位置:网站首页>Mysql database, de duplication, connection
Mysql database, de duplication, connection
2022-07-24 18:49:00 【The end of the world and you】
Mysql database , duplicate removal , Connecting article
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 , Matching data rows from another table involves 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)
边栏推荐
- PCI express physical layer - electrical part
- OPENGL学习(五)Modern OpenGL 三角形绘制
- Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)
- We have to understand the four scopes: application, session, request and page
- CF. Bits And Pieces(子集状压dp + 剪枝)
- 为什么梯度是函数变化最快的方向
- Ionic4 learning notes 1
- 网络安全80端口—-PHP CGI参数注入执行漏洞
- Today's sleep quality record 79 points
- matplotlib
猜你喜欢

32-bit stack overflow advanced

Rookie colleagues cost me 2K. Did you recite the secret of salary increase? (collect it quickly!)

为什么梯度是函数变化最快的方向

ETL开发工具Kettle下载安装环境搭建及使用教程

matplotlib

Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag

Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)

Leetcode memory deep search / dynamic planning V2

今日睡眠质量记录79分

Mysqlworkbench performance analysis tool -- Performance dashboard
随机推荐
[Tkinter] layout management and event system
JVM方法调用
Oracle EBS form common objects and their relationships
Ionic4 Learning Notes 6 -- using native ionic4 components in custom components
QT - animation frame
Those gods on Zhihu reply
理解corners_align,两种看待像素的视角
CF lomsat gelral (heuristic merge)
C Programming classic tutorial
Create parent-child projects in clion (cmake tool) and introduce the method of third-party libraries
多线程与并发编程常见问题(未完待续)
04-分布式资源管理系统YARN
OpenGL learning (IV) glut 3D image rendering
Serial vector format (SVF) file format
Nacos introduction and console service installation
OpenGL learning (III) glut two-dimensional image rendering
无关的表进行关联查询及null=null条件
Redis data type
Mysqlworkbench performance analysis tool -- Performance dashboard
知乎上的那些神回复