当前位置:网站首页>MySQL - composite query external connection
MySQL - composite query external connection
2022-07-23 13:12:00 【4nc414g0n】
Composite query Inside and outside connection
Composite query
The basic query
Be careful:MySQL in ,select What comes out is also a watch , You can use... In a query statementThere is an employee list emp:
Example :
- Check that the salary is higher than 500 Or position is MANAGER Of employees , At the same time, their initials should be capitalized J
- Sort by department number in ascending order and employee's salary in descending order
- Use annual salary to sort in descending order
- Displays the name and position of the highest paid employee
- Displays information about employees whose wages are higher than the average
- Displays the average wage and maximum wage for each department
- It shows that the average wage is lower than 2000 Your department number and its average salary
Be careful: Not hereselect deptno, avg(sal) as 'avgsal' from emp group by deptno where avg(sal)<2000;,where It must be group by Beforeand where Is the first operation , Obviously not in line with the meaning of the question , You should use having- Displays the total number of employees for each position , Average wage
Multi-table query
There's a database scott There are three tables in it :dept emp salgrade
select count(*) from dept;select count(*) from emp;select count(*) from salgrade;Their Cartesian product :
The so-called Cartesian product , Just at the mathematical level , Enumerate all the data combinations of multiple tables ,But it doesn't mean , All new data records , It all makes sense,The next step is to screen
Finish the Cartesian product , The essence is that we put all relevant data , Aggregate to a table , The next question , It becomes a single table query , According to the problem surface , Which tables are the data that need to be traced + Correlation fieldExample :
- Show employee name 、 The employee's salary and the name of the Department
- The display department number is 10 The name of your department , Employee name and salary
- Show the name of each employee , Wages , And salary level
Self join
Self join: It refers to the connection query in the same table
Example :
- Show employees FORD The number and name of the superior
Subquery
Subquery: Embedded in other sql Statement select sentence , Also called nested query
Single line sub query
Same as employee table
Example :
- Show SMITH Employees in the same department
Multi line sub query
in keyword : In which
- Query and 10 The name of the employee with the same position in department No , Position , Wages , Department number , But it doesn't include 10 Their own
all keyword : Must satisfy any
- Show salary ratio Department 30 The names of all the employees with high salaries 、 Salary and department number
any keyword : Satisfy any one of them
- Show salary ratio Department 30 Name of any employee with a higher salary 、 Salary and department number ( Including employees in their own department )
Multi column subquery
There are more than 1 Column names , Such as sub query
where (ename, sal)=(select ename,sal from emp)
Example :
- Query and SMITH All employees in the same department and position , Not included SMITH I am
from Clause
Use a subquery as a temporary table( Be careful , First, according to the requirements of the topic , First confirm the table of sub query )
Example :
Displays the name of each employee whose salary is higher than the average salary of his / her department 、 department 、 Wages 、 Average wage
- 1. List the average salary of all departments :
select deptno, avg(sal) from emp;
- 2. Filter out two from the Cartesian product deptno The same data is valid data
- 3.
Find the name of the highest paid person in each department 、 Wages 、 department 、 Maximum wage
Display information for each department ( Department name , Number , Address ) And the number of people
- 1. Subquery
- 2. Multiple tables
Pay attention to the pit here , front select There are specific column names ,group by Also be sure to bring
Merge query
Merge multiple select The results of the implementation of , You can use the set operator union,union all
union: Used to obtain the union of two result sets . When using this operator , Duplicate lines in the result set will be automatically removed
- Example : Pay more than 2500 Or position is MANAGER Find out
union all: This operator is used to get the union of two result sets . When using this operator , Duplicate rows in the result set are not removed
- Example : Pay more than 25000 Or position is MANAGER Find out
Inside and outside connection
Internal connection
utilize where Clause to filter the Cartesian product formed by the two tables , That is, the writing method used before
Standard writing :
select Field from surface 1 inner join surface 2 on Connection condition and Other conditions
External connection
There are two tables :
The left outer join
If the union query , The table on the left shows that it is a left outer join
Standard writing :select Field name from Table name 1 left join Table name 2 on Connection condition
- Example : Check the grades of all the students , If the student has no grades , Also display the student's personal information
Right connection
If the union query , The table on the right shows that we call it right outer join
Standard writing :select Field from Table name 1 right join Table name 2 on Connection condition ;
Example :
- Yes stu Table and exam Table union query , Show all your grades , Even if there are no students corresponding to this grade , Show it, too
- List Department names and employee information for these departments , Also list departments without employees
right join Empathy , Just exchange the positions of two meters
边栏推荐
- linx的链接、一级目录、重定向、cp与mv
- 常见的定时任务Scheduled cron 表达式
- C randomly generate a score to judge its grade (excellent, good, medium, poor, failed)
- Intercept the specified range data set from list < map >
- Query the cross compiled executable dependency Library
- 信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)
- Why build a local Yum warehouse?
- Numpy:基本操作快速入门
- Default routing configuration instance learning record
- Rk3588 compilation problem set
猜你喜欢

基于redis+lua进行限流

Count different types of data according to different times (stored procedures)

Signal integrity (SI) power integrity (PI) learning notes (XXXI) power distribution network (III)

CAN控制器的位同步过程

In the Internet era, how to refine user operations?

在GPU上运行MATLAB程序

互联网时代下,如何精细化用户运营?

Plug ins used by Jenkins

HCIA----06 OSPF

EasyGBS平臺出現錄像無法播放並存在RTMP重複推流現象,是什麼原因?
随机推荐
Numpy: quick start to basic operations
Redis如何实现持久化?详细讲解RDB的三种触发机制及其优缺点,带你快速掌握RDB
国信证券软件开户是安全吗?信息会不会泄露?
How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
Signal integrity (SI) power integrity (PI) learning notes (32) power distribution network (4)
C output Fibonacci sequence
TI单芯片毫米波雷达代码走读(二十五)—— 角度维(3D)处理流程
Convert the specified seconds to minutes and seconds
C语言-大端存储和小端存储
C language - big end storage and small end storage
ACL access control experiment
[ACTF2020 新生赛]BackupFile 1
静态扩展配置
北汇信息12岁啦|Happy Birthday
STP configuration instance learning record
HCIA----04 路由静态扩展、VLAN
Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
HCIA----07 ACL-Net
Common CMD commands to quickly open programs
Harbor deployment





























