当前位置:网站首页>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 ?

  1. First of all : High execution efficiency .
  2. 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;

image-20220226164334602

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;

image-20220226164637647

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;

image-20220226171420447

  • 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;

image-20220226173627319

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 :

  1. Find out the average salary
select avg(sal) from emp;
  1. where Filter
select * from emp where sal > 2073.214286;
  1. 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
  1. 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;
  1. 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;
原网站

版权声明
本文为[BKSW.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281622108767.html