当前位置:网站首页>Review of MySQL (V): Joint table query and sub query
Review of MySQL (V): Joint table query and sub query
2022-06-12 18:20:00 【BKSW.】
MySql Review ( 5、 ... and ): Join table query and sub query
Query result de duplication
On the de duplication of query results :distinct keyword
select distinct job from emp; // distinct Keyword to remove duplicate records .
distinct Keywords can only be used before all fields .
Wrong writing :select ename,distinct job from emp;
Write it correctly :select distinct deptno,job from emp;
About table aliases
About table aliases :
select e.ename,d.dname from emp e,dept d;
What are the benefits of table aliases ?
- First of all : High execution efficiency .
- second : Good readability .
Classification of join queries
According to the age of grammar
- SQL92: Older syntax
- SQL99: Relatively new grammar
SQL92 grammar
Internal connection
- Display information for each employee , And display the name of the Department
select emp.ename, dept.dname from emp, dept where emp.deptno=dept.deptno;
-- You can also use aliases
select e.ename, d.dname from emp e, dept d where e.deptno=d.deptno;
Query only equal data ( Data with equal connection conditions )
Self join
Only one table is connected , Specific query methods , Think of one table as two , As shown in the following example : The first table emp e Code the employee table ,emp m Represents the leadership table , It is equivalent to the employee table and the Department table
- Get the name of the employee and the leader
select e.ename, m.ename from emp e, emp m where e.mgr=m.empno;
SQL99 grammar
- ( Internal connection ) Show salary greater than 2000 Employee information , And display the name of the Department
-- use SQL92 grammar :
select e.ename, e.sal, d.dname from emp e, dept d where e.deptno=d.deptno and e.sal > 2000;
-- use SQL99 grammar :
select e.ename, e.sal, d.dname from emp e join dept d on e.deptno=d.deptno where e.sal>2000;
-- or :( But in practice, it is generally not added inner keyword )
select e.ename, e.sal, d.dname from emp e inner join dept d on e.deptno=d.deptno where e.sal>2000;
- ( External connection ) Show employee information , And display the name of the Department , If a department has no employees , Then the Department must also show
-- The right connection :
select e.ename, e.sal, d.dname from emp e right join dept d on e.deptno=d.deptno;
-- Left connection :
select e.ename, e.sal, d.dname from dept d left join emp e on e.deptno=d.deptno;
-- The above two queries have the same effect
According to the connection mode of the table
Internal connection
Equivalent connection
Non equivalent connection
Self join
External connection
The left outer join ( Left connection )
Right connection ( The right connection )
Full connection
Equivalent connection
The condition is the equivalence relationship
- Query the Department name of each employee . Employee name and department name are required to be displayed
-- SQL92:( excessive , A: no, no )
select
e.ename,d.dname
from
emp e, dept d
where
e.deptno = d.deptno;
-- SQL99:( frequently-used )
select
e.ename,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno;
// inner Can be omitted , With inner The goal is to be more readable .
select
e.ename,d.dname
from
emp e
inner join
dept d
on
e.deptno = d.deptno;
grammar :
...
A
join
B
on
Connection condition
where
...
Non equivalent conditions
The relationship in the connection condition is an unequal relationship .
- Find out the salary grade of each employee , Ask to show employee name 、 Wages 、 Pay scale .
select
e.ename,e.sal,s.grade
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal;
-- inner May not omit
select
e.ename,e.sal,s.grade
from
emp e
inner join
salgrade s
on
e.sal between s.losal and s.hisal;
Self join
- Find the employee and the corresponding superior
select
a.ename as ' Employee name ',b.ename as ' Name of leader '
from
emp a
inner join
emp b
on
a.mgr = b.empno;
External connection
What is external connection , What is the difference between internal connection and internal connection ?
The most important feature is : All the data in the main table can be found unconditionally
Internal connection :
hypothesis A and B Tables to connect , If you use an internal connection , Anyone who A Table and B The table can be queried by matching the records on the table , This is the inner link .
AB There is no difference between the two tables , The two tables are equal .
External connection :
hypothesis A and B Tables to connect , If you use external connections ,AB One of the two tables is the main table , A watch is a secondary watch , The main query is in the main table
The data of , With the side table , When the data in the secondary table does not match the data in the primary table , The secondary table automatically simulates NULL To match .
Classification of external connections ?
The left outer join ( Left connection ): The table on the left is the main table .
Right connection ( The right connection ): The table on the right is the main table .
The left connection has the right connection , The right connection will also have the writing method of the corresponding left connection .
- Find the superior of each employee , All employees' must be queried
-- Internal connection :
select
a.ename ' staff ', b.ename ' Leader '
from
emp a
join
emp b
on
a.mgr = b.empno;
-- External connection :( The left outer join / Left connection )
select
a.ename ' staff ', b.ename ' Leader '
from
emp a
left join
emp b
on
a.mgr = b.empno;
-- outer It can be omitted .
select
a.ename ' staff ', b.ename ' Leader '
from
emp a
left outer join
emp b
on
a.mgr = b.empno;
-- External connection :( Right connection / The right connection )
select
a.ename ' staff ', b.ename ' Leader '
from
emp b
right join
emp a
on
a.mgr = b.empno;
-- outer It can be omitted .
select
a.ename ' staff ', b.ename ' Leader '
from
emp b
right outer join
emp a
on
a.mgr = b.empno;
- Find out which department has no employees
select
d.*
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
e.empno is null;
Three tables join to query
- Find out the Department and salary grade of each employee
-- Be careful , Explain it. :
-- ....
-- A
-- join
-- B
-- join
-- C
-- on
-- ...
-- Express :A Table and B Tables are connected first , After the connection A The table continues with C Tables to connect .
select
e.ename,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;
- Find out the Department name of each employee 、 Salary scale and superior leaders
select
e.ename ' staff ',d.dname ' Department name ',s.grade ' Pay scale ',e1.ename ' Leader's name '
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 e1
on e.mgr = e1.empno;
Subquery
Subqueries are nested select sentence , It can be understood that a subquery is a table
select Nested in statements select sentence , Nested select Statements are subqueries .
Where can subqueries appear ?
select
..(select).
from
..(select).
where
..(select).
where Clause using subqueries
Find information about employees who are above average pay
Wrong writing :
select * from emp where sal > avg(sal); // The wrong way to write ,where You can't use the grouping function directly .
Write it correctly :
- Find out the average salary
select avg(sal) from emp;
- where Filter
select * from emp where sal > 2073.214286;
- Merge one or two steps
select * from emp where sal > (select avg(sal) from emp);
from After nested sub query
- Find out the average salary grade of each department
- Find out the average salary of each department ( According to the department number , seek sal Average value )
select deptno,avg(sal) as avgsal from emp group by deptno;
- Use the above results as a temporary table t, Let table t and salgrade s Connected to a
select
t.*,s.grade
from
(select deptno,avg(sal) as avgsal from emp group by deptno) t
join
salgrade s
on
t.avgsal between s.losal and s.hisal;
select The following nested subquery statement
- Find out the name of each employee's Department , Employee name and department name are required
-- Method 1 :
select
e.ename,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno;
-- Method 2 :
select
e.ename,(select d.dname from dept d where e.deptno = d.deptno) as dname
from
emp e;
边栏推荐
- 【sql语句基础】——查(select)(单表查询)
- VirtualLab基礎實驗教程-4.單縫衍射
- 机器学习系列(3):Logistic回归
- Use applet to quickly generate app in seven steps
- Typescript advanced type (2)
- String s = null ; String s = new String(); String s = "; what is the difference between string s?
- Stream流注意点
- 用grep awk提取字符串
- Lightweight and convenient small program to app technology solution to realize interconnection with wechat / traffic app
- Gossip about the 88 of redis source code
猜你喜欢
VirtualLab基础实验教程-6.闪耀光栅
1.5 what is an architect (serialization)
GD32F4xx控制DGUS 变量显示
网盘和对象云存储管理之磁盘映射工具比较
TypeScript常用类型(一)
General differences between SQL server versions released by Microsoft in different periods so far, for reference
"Big fat • small lesson" - talk about big file segmentation and breakpoint sequel
Applet and app are owned at the same time? A technical scheme with both
Extreme Programming -- Practice of root cause analysis
Title 37: sorting 10 numbers
随机推荐
309. the best time to buy and sell stocks includes the freezing period
JS judge palindromes
JS中的数组(含leetcode例题)<持续更新~>
Gospel of audio and video developers, rapid integration of AI dubbing capability
Gospel of audio and video developers, rapid integration of AI dubbing capability
js二分法
Typescript common types (I)
torch.where的新用法(很老但是大家忽略的用法)
leetcode 300. Longest increasing subsequence
Vant3+ts H5 pages are nested into apps to communicate with native apps
Summary of interview questions
Random talk about redis source code 91
MySQL learning notes
TypeScript类型声明文件(三)
An easy-to-use IDE for small programs
网盘和对象云存储管理之磁盘映射工具比较
USB转串口那些事儿—串口驱动类型
Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
Nixos 22.05 installation process record
Relationship between resolution and line field synchronization signal