当前位置:网站首页>5. Multi table query
5. Multi table query
2022-07-26 07:17:00 【Piglet vs Hengge】
Catalog
01、 Merge result set :UNION、UNION ALL
1、UNION: Remove duplicate records
2、UNION ALL: Do not remove duplicate records
3.1 Pay more than JONES The employees'
3.2 Query and SCOTT Employees in the same department
3.3 Pay more than 30 Employee information of department No
3.4 Inquire about work and salary and MARTIN( Martin ) Exactly the same employee information
3.5 Yes 2 Employee information of more than direct reports
Self join : Connect yourself , names
3.7 seek 7369 Employee number 、 full name 、 Manager number and manager name
3.8 Ask for all information about the highest paid employees in all departments
00、 Multi-table query
There are several kinds of multi table queries :
1、 Merge result set ;UNION、UNION ALL
2、 Link query
2.1 Internal connection [INNER] JOIN ON
2.2 External connection OUTER JOIN ON
The left outer join LEFT [OUTER] JOIN
Right connection RIGHT [OUTER] JOIN
Full outer join (MySQL I won't support it )FULL JOIN
2.3 Natural join NATURAL JOIN
3、 Subquery
01、 Merge result set :UNION、UNION ALL
(1) effect : To merge a result set is to combine two select The query results of the statement are combined !
(2) There are two ways to merge result sets :
UNION: Remove duplicate records
UNION ALL: Do not remove duplicate records
(3) The two results of the merger : Number of columns 、 The column type must be the same .Premise : Create data table t1 and t2:
# Create data table t1 and t2 #(1) Create data table t1, And insert data CREATE TABLE t1( `a` INT, `b` VARCHAR(10) ); INSERT INTO t1 VALUES(1,'a'),(2,'b'),(3,'c'),(4,'d'); DELETE FROM t1 #(1) Create data table t2, And insert data DROP TABLE t2; CREATE TABLE t2( `c` INT, `d` VARCHAR(10) ); INSERT INTO t2 VALUES(4,'d'),(5,'e'),(6,'g');
1、UNION: Remove duplicate records
#(1)UNION: Remove duplicate records
SELECT * FROM t1
UNION
SELECT * FROM t2
--> final result :
mysql> SELECT * FROM t1
-> UNION
-> SELECT * FROM t2;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
| 6 | g |
+------+------+2、UNION ALL: Do not remove duplicate records
(2)UNION ALL: Do not remove duplicate records
SELECT * FROM t1
UNION ALL
SELECT * FROM t2
--> final result
mysql> SELECT * FROM t1
-> UNION ALL
-> SELECT * FROM t2;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 4 | d |
| 5 | e |
| 6 | g |
+------+------+02、 Link query
Join query is to find the product of multiple tables , for example t1 Connect t2, So the result of the query is t1*t2.
Join queries produce Cartesian Products , That is, a lot of error messages will be generated , It needs to be filtered , Filter through primary and foreign key relationships 、
(1) Link query
(2) Internal connection
(3) External connection
(4) Natural join
#2. Link query :
/*
Join queries produce Cartesian Products , That is, a lot of error messages will be generated , It needs to be filtered , Filter through primary and foreign key relationships
*/
# Create data table t1、t2, And insert data , Make the following connection query
# produce t1*t2 data
SELECT * FROM t1,t2;
# operation emp dept
SELECT * FROM emp,dept;
# Use the master foreign key relationship as a condition to remove useless information
SELECT * FROM emp,dept WHERE emp.deptno=dept.deptno;
# The above query results will query all columns of the two tables , Maybe you don't need so many columns , Now you can specify the columns to query .
SELECT emp.empno,emp.ename,emp.sal,dept.deptno,dept.loc FROM emp,dept WHERE emp.deptno=dept.deptno;
# Aliasing tables :
SELECT e.empno,e.ename,e.sal,d.deptno,d.loc FROM emp e,dept d WHERE e.deptno=d.deptno;
#2.1 Internal connection
/*
Characteristics of internal connection : The query results must meet the conditions . For example, we ask emp Insert a record in the table :
1015 Zhang San Cleaner 1009 1999-12-31 80000.00 50
among deptno by 50, And in the dept Only in the table 10、20、30、40 department , So the above query
The result will not appear “ Zhang San ” This record , Because it can't satisfy e.deptno=d.deptno This condition .
*/
# Inner connection standard statement : The internal connection will not display records that do not meet the conditions
SELECT * FROM emp e INNER JOIN dept d ON e.deptno=d.deptno;
# Before use emp Add another row of data to the table :
#2.2 External connection : Records that do not meet the conditions will be displayed
#(1) The left outer join : First, query all the data in the left table , Then go to the right table , The data satisfied in the right table shows , Dissatisfied display NULL
SELECT * FROM emp e LEFT OUTER JOIN dept d ON e.deptno=d.deptno;
#(2) Right connection : First, query all the data in the right table , Then go to the left table , The data satisfied in the left table shows , Dissatisfied display NULL
SELECT * FROM emp e RIGHT OUTER JOIN dept d ON e.deptno=d.deptno;
#2.3 Natural join : Automatically find filter conditions to remove useless data in Cartesian product
# Natural internal connection
SELECT * FROM emp NATURAL JOIN dept;
# Natural left outer connection
SELECT * FROM emp NATURAL LEFT JOIN dept;
# Natural right outer connection
SELECT * FROM emp NATURAL RIGHT JOIN dept;03、 Subquery
Subquery ( nested queries ): One select The statement contains another complete select sentence
3.1 Pay more than JONES The employees'
#3.1 Pay more than JONES The employees'
# First step : Inquire about JONES The salary of -->2975
SELECT sal FROM emp WHERE `ename`='JONES';
# The second step : Query salary greater than 2975 Employee records of
SELECT * FROM emp WHERE `sal`>2975;
# Merge write
SELECT * FROM emp WHERE `sal`>(SELECT sal FROM emp WHERE `ename`='JONES');
--> final result :
mysql> SELECT * FROM emp WHERE `sal`>(SELECT sal FROM emp WHERE `ename`='JONES');
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+-----------+------+------------+---------+------+--------+3.2 Query and SCOTT Employees in the same department
#3.2 Query and SCOTT Employees in the same department
# First step : Inquire about SCOTT Department number of -->20
SELECT deptno FROM emp WHERE `ename`='SCOTT';
# The second step : Inquiry Department No 20 Employee records of
SELECT * FROM emp WHERE `deptno`=20;
# Merge write
SELECT * FROM emp WHERE `deptno`=(SELECT deptno FROM emp WHERE `ename`='SCOTT');
--> final result :
mysql> SELECT * FROM emp WHERE `deptno`=(SELECT deptno FROM emp WHERE `ename`='SCOTT');
+-------+-------+---------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+---------+------+------------+---------+------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+---------+------+------------+---------+------+--------+3.3 Pay more than 30 Employee information of department No
#3.3 Pay more than 30 Employee information of department No
# First step : Find out first 30 The highest salary in department No -->2850
SELECT MAX(sal) FROM emp WHERE `deptno`=30;
# The second step : Pay more than 30 Employee information of department No
SELECT * FROM emp WHERE `sal`>2850;
# Merge write :
SELECT * FROM emp WHERE `sal`>(SELECT MAX(sal) FROM emp WHERE `deptno`=30);
# Another way of writing
SELECT * FROM emp WHERE `sal`>ALL(SELECT MAX(sal) FROM emp WHERE `deptno`=30);
--> final result :
mysql> SELECT * FROM emp WHERE `sal`>(SELECT MAX(sal) FROM emp WHERE `deptno`=30);
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
+-------+-------+-----------+------+------------+---------+------+--------+3.4 Inquire about work and salary and MARTIN( Martin ) Exactly the same employee information
#3.4 Inquire about work and salary and MARTIN( Martin ) Exactly the same employee information
# First step : Inquire about MARTIN Work and salary -->
SELECT job,sal FROM emp WHERE `ename`='MARTIN';
# The second step : Inquire about work and salary and MARTIN( Martin ) Exactly the same employee information
SELECT * FROM emp WHERE `job`='SALESMAN' AND `sal`=1250.00;
# Merge write
SELECT * FROM emp WHERE (job,sal)
IN(SELECT job,sal FROM emp WHERE `ename`='MARTIN');
--> final result :
mysql> SELECT * FROM emp WHERE (job,sal)
-> IN(SELECT job,sal FROM emp WHERE `ename`='MARTIN');
+-------+--------+----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+---------+---------+--------+
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
+-------+--------+----------+------+------------+---------+---------+--------+3.5 Yes 2 Employee information of more than direct reports
#3.5 Yes 2 Employee information of more than direct reports
# First step : First, find out mgr More than twice -->7698 7839
SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(mgr)>2
# The second step : Yes 2 Employee information of more than direct reports
SELECT * FROM emp WHERE `empno`=7698 OR `empno`=7839;
# Merge write
SELECT * FROM emp WHERE empno IN(SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(mgr)>2);
--> final result :
mysql> SELECT * FROM emp WHERE empno IN(SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(mgr)>2);
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
+-------+-------+-----------+------+------------+---------+------+--------+3.6 Query the employee number as 7788 The name of the employee 、 Wages 、 Department name 、 Department Address
#3.6 Query the employee number as 7788 The name of the employee 、 Wages 、 Department name 、 Department Address
SELECT ename,sal,dname,loc FROM emp,dept WHERE emp.deptno=dept.deptno AND empno=7788
# Now? dept In this table, only 3 Column , And this 3 Columns are all we need . If dept This watch has 100 Column , We only need one deptno、dname、loc this 3 Column , So we're in from You can use subqueries later
SELECT e.ename,e.sal,d.dname,d.loc
FROM emp e,(SELECT dname,loc,deptno FROM dept)d
WHERE e.deptno=d.deptno AND e.empno=7788
--> final result ;
mysql> SELECT e.ename,e.sal,d.dname,d.loc
-> FROM emp e,(SELECT dname,loc,deptno FROM dept)d
-> WHERE e.deptno=d.deptno AND e.empno=7788;
+-------+---------+----------+--------+
| ename | sal | dname | loc |
+-------+---------+----------+--------+
| SCOTT | 3000.00 | RESEARCH | DALLAS |
+-------+---------+----------+--------+Self join : Connect yourself , names
3.7 seek 7369 Employee number 、 full name 、 Manager number and manager name
#3.7 seek 7369 Employee number 、 full name 、 Manager number and manager name
SELECT empno,ename,mgr,ename FROM emp WHERE empno=7369
SELECT ename FROM emp WHERE empno=7902 # Get the name of the manager according to the obtained manager number
SELECT e1.empno AS ' Employee number ',e1.ename AS ' Employee name ',e1.mgr ' Employee's manager number ',e2.ename AS ' Employee's manager name ' FROM emp e1,emp e2
WHERE e1.empno=7369 AND e1.mgr=e2.empno
--> final result :
mysql> SELECT e1.empno AS ' Employee number ',e1.ename AS ' Employee name ',e1.mgr ' Employee's manager number ',e2.ename AS ' Employee's manager name ' FROM emp e1,emp e2
-> WHERE e1.empno=7369 AND e1.mgr=e2.empno;
+----------+----------+----------------+----------------+
| Employee number | Employee name | Employee's manager number | Employee's manager name |
+----------+----------+----------------+----------------+
| 7369 | SMITH | 7902 | FORD |
+----------+----------+----------------+----------------+3.8 Ask for all information about the highest paid employees in all departments
#3.7.2 Ask for all information about the highest paid employees in all departments
# Calculate the maximum salary corresponding to each department according to the department number
SELECT deptno,MAX(sal) FROM emp GROUP BY deptno;
# Not rigorous enough : All information about the highest paid employees in all departments
SELECT * FROM emp WHERE sal=5000 OR sal=3000 OR sal=2850 OR sal=1200;
SELECT * FROM emp WHERE sal IN(SELECT MAX(sal) FROM emp GROUP BY deptno)
# The following commands are recommended : rigorous
SELECT e1.* FROM emp e1,(SELECT MAX(sal) maxsal,deptno FROM emp GROUP BY deptno)d
WHERE e1.sal=d.maxsal AND e1.deptno=d.deptno;
--> final result
mysql> SELECT e1.* FROM emp e1,(SELECT MAX(sal) maxsal,deptno FROM emp GROUP BY deptno)d
-> WHERE e1.sal=d.maxsal AND e1.deptno=d.deptno;;
+-------+-------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-------+-----------+------+------------+---------+------+--------+
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7988 | LISA | Procter & Gamble | 7782 | 2022-07-22 | 1200.00 | NULL | 50 |
+-------+-------+-----------+------+------------+---------+------+--------+边栏推荐
- shape 和 size() 区别
- RGB-T追踪——【数据集基准】GTOT / RGBT210 / RGBT234 / VOT-2019-2020 / LasHeR / VTUAV
- 【QT】详解 *.pro、*.pri、*.prf、*.prl文件
- Apache DolphinScheduler&TiDB联合Meetup | 聚焦开源生态发展下的应用开发能力
- Unity3d asynchronous loading of scenes and progress bar loading
- Differences in the use of function call pointer parameters *p, * & P
- NPM command
- How to convert multi row data into multi column data in MySQL
- Hands on practice - teach you how to make an intelligent fish tank with STM32
- Yolov6 target detection practice: training your own data set (video tutorial)
猜你喜欢

DaemonSet

Hands on practice - teach you how to make an intelligent fish tank with STM32

Drools (4): drools basic syntax (2)

What are the basics of getting started with spot silver

3.0.0 alpha 重磅发布!九大新功能、全新 UI 解锁调度系统新能力

File server fastdfs

NFT数字藏品系统开发:华为发布首款珍藏版数字藏品

Opencv learn resize and crop

Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?

Apache dolphin scheduler version 3.0.0-beta-1 was released, and flinksql and Zeppelin task types were added
随机推荐
What are the basics of getting started with spot silver
Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output
Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
Qt:模态非模态、文本框、按钮、单行输入框
Pycharm的相关配置:改字体样式和大小、更改图片背景、更改控制台输出的字体颜色
centos7下的MySQL57版本,遇到一个问题不理解有知道的大shen告知
20220725 compensator in automatic control principle
Leetcode:1898. maximum number of removable characters [if you want to delete some IDX from a pile of things, don't use pop]
Opencv learning color detection
MySQL安装教程-手把手教你安装
5、多表查询
基于C51实现led流水灯
Deep learning visualization
NFT数字藏品系统开发:华为发布首款珍藏版数字藏品
Why can't extern compile variables decorated with const?
Deep learning learning notes -- solve the problem of slow download of CONDA and pip
What are the ways to open the JDBC log of Youxuan database
Opengauss simple version installation error
又是一年开源之夏,1.2万项目奖金等你来拿!
ModuleNotFoundError: No module named ‘pip‘解决办法