当前位置:网站首页>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;
边栏推荐
- JS quick sort
- JS for Fibonacci sequence
- Can tonghuashun open an account? Can tonghuashun directly open the security of securities companies on the app
- Schematic diagram of active differential crystal oscillator and differences among lv-pecl, LVDS and HCSL
- Introduction to reinforcement learning and analysis of classic items 1.3
- General differences between SQL server versions released by Microsoft in different periods so far, for reference
- leetcode 718 最长公共子串
- Title 66: input 3 numbers a, B, C, and output them in order of size.
- Virtual Lab Basic Experiment tutoriel - 4. Diffraction à fente unique
- js快速排序
猜你喜欢

VirtualLab基础实验教程-5.泊松亮斑

VirtualLab basic experiment tutorial -6 Blazed grating

Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default

Vant3+ts encapsulates uploader upload image component

High-Speed Layout Guidelines 未完...

Introduction to service grid and istio - continued

ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间

VirtualLab基础实验教程-4.单缝衍射

JS sum of two numbers

js求斐波那契数列
随机推荐
Self made calculator (1 realized by Boolean logic operation unit and control unit programming)
leetcode491 递增子序列
JS quick sort
Applet and app are owned at the same time? A technical scheme with both
Section qemu+gdb
ESP-IDF 添加自己的组件
VirtualLab basic experiment tutorial -4 Single slit diffraction
leetcode 300. Longest increasing subsequence
js判断回文数
Global lock, table lock, row lock
Machine learning series (3): logistic regression
Gospel of audio and video developers, rapid integration of AI dubbing capability
A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
Is it safe to open an account in flush
Use applet to quickly generate app in seven steps
js将数组中的0移动到末尾
js求斐波那契数列
Changes in the third generation dri
Remote gadget putty (Alibaba cloud mirror station address sharing)
Typescript advanced type (2)