Chapter 6 Compound Query
- I. Introduction
- Two, Cartesian product
- Three, multi-table query
- 4. Self-connection
- Five, sub query
-
- 1. What is a subquery
- 2. Single row subquery
- 3. Multi-line subquery
- 4. Multi-column subquery
- 5. Use subqueries in the from clause
-
- (1) The query result is used as a temporary table
- (2) Example
-
- Display the name, department, salary, and average salary of each employee whose salary is higher than the average salary of his department
- Find the name, salary, department, highest salary of the person with the highest salary in each department
- Display the information of each department (department name, number, address) and the number of personnel
- 6. Merge query
I. Introduction
In the previous chapters, we introduced single-table queries, but in this chapter we are going to explain multi-table queries.
Two, Cartesian product
Let A and B be sets, use the elements in A as the first element, and the elements in B as the second element to form an ordered pair. The set of all such ordered pairs is called the Cartesian product of A and B, denoted as AxB.
The symbolization of the Cartesian product is:
A x B = < x , y > ∣ x ∈ A ∧ y ∈ B AxB={<x,y>|x∈A∧y∈B} A x B=<x,y>∣x∈A∧y∈B
For example, A = a , b , B = 0 , 1 , 2 A={a,b},B={0,1,2}A=a,b,B=0,1,2 , then
A x B = < a , 0 > , < a , 1 > , < a , 2 > , < b , 0 > , < b , 1 > , < b , 2 > AxB={<a,0>,<a,1>,<a,2>,<b,0>,<b,1>,<b,2>} A x B=<a,0>,<a,1>,<a,2>,<b,0>,<b,1>,<b,2>
B x A = < 0 , a > , < 0 , b > , < 1 , a > , < 1 , b > , < 2 , a > , < 2 , b > BxA={<0,a>,<0,b>,<1,a>,<1,b>,<2,a>,<2,b>} B x A=<0,a>,<0,b>,<1,a>,<1,b>,<2,a>,<2,b>
Three, multi-table query
1. Understanding of multi-table query
When it comes to multi-table queries, MySQL will splice the tables in the form of Cartesian product, and finally splice them into one table. So on the surface we are querying multiple tables, but in fact we are still querying a single table.
2. Cartesian product and multi-table splicing
For example, we now have two tables A and B, then mysql will splice each statement in table A with all the statements in table B when splicing. That is to say, all possibilities of record splicing will be listed eventually.
When mysql is spliced, the so-called multi-table query is converted into a single-table query.
However, since mysql lists all splicing possibilities, it is inevitable that there will be some illogical records in the splicing results, and we need to perform appropriate screening at this time.
3. Multi-table query example
We are using a new database here: scott.
There are three tables in this database: dept, emp, and salgrade
are described as follows:
Description of table dept:
Description of table emp:
Description of table salgrade:
(1) Display the name of the employee, the salary of the employee, and the name of the department
Because the above data comes from the EMP and DEPT tables, it needs to be queried jointly.
Let's first look at the respective contents of the two tables, and then look at the contents of the spliced two tables.
The content of the emp table is as follows:
The content of dept is as follows:
Now splicing the two tables, the result is as follows (because the result of splicing is very long, so only part of it is shown):
There is a shortcoming in the above table. We found that there are two tables in the table. column deptno, but the values corresponding to some records in these two columns are not equal. These unequal data are what we want to filter out.
So we can use the following statement to filter:
select * from emp, dept where emp.deptno = dept.deptno;
Here the syntax format like: 表的名称.变量名
is very similar to a structure in c or a class in c++.
When we filter the data in the table correctly, we can perform single-table query.
Our requirement just now is to display the name of the employee, the salary of the employee and the name of the department
So you can use the following statement to filter:
select ename, sal,dname from emp, dept where emp.deptno = dept.deptno;
(2) Display the department name, employee name and salary of the department number 10
select dname,ename,sal from emp, dept where emp.deptno = dept.deptno and emp.deptno = 10;
(3) Display the name, salary, and salary level of each employee
select ename,sal,grade from emp, salgrade where emp.sal between salgrade.losal and salgrade.hisal;
4. Self-connection
1. What is self-connection
Self-join refers to the join query of the same table, that is, Cartesian product operation between itself and itself.
2. Example of self-connection
Display the number and name of the superior leader of the employee FORD (mgr is the number of the employee leader – empno)
(1) Method 1: Single table query:
select empno, ename from emp where emp.empno = (select mgr from emp where ename = 'FORD');
(2) Method 2: Multi-table query:
select leader.empno, leader.ename from emp leader, emp worker where worker.mgr = leader.empno and worker.ename = 'FORD';
The way to alias the table is used here.
Five, sub query
1. What is a subquery
A subquery is a select statement embedded in other SQL statements, also called a nested query. In fact, when we just introduced self-join, the first method used in the example is subquery.
2. Single row subquery
example
Show employees in the same department as SMITH
select * from emp where deptno = (select deptno from emp where ename = 'SMITH');
3. Multi-line subquery
A multi-row subquery is a subquery that returns multiple rows of records.
(1) in keyword
Query the name, position, salary, and department number of the employees who have the same job position as department 10, but do not include 10's own.
select ename, job, sal, deptno from emp where job in (select job from emp where deptno = 10) and deptno != 10;
(2) all keyword
Displays the name, salary, and department number of employees whose salary is higher than that of all employees in department 30.
select ename,sal,deptno from emp where sal > all (select sal from emp where deptno = 30);
(3) any keyword
Display the name, salary, and department number of employees whose salary is higher than that of any employee in department 30 (including employees in their own department)
select ename, sal, deptno from emp where sal > any(select sal from emp where deptno = 30);
4. Multi-column subquery
A single-row subquery refers to a subquery that returns only a single column and a single row of data; a multi-row subquery refers to a single-column multi-row data returned, all for a single column, while a multi-column subquery refers to a subquery that returns multiple columns of data statement.
(1) Example
Query all employees who have the same department and position as SMITH, excluding SMITH himself.
select * from emp where (deptno, job) = (select deptno, job from emp where ename = 'SMITH') and ename != 'SMITH';
5. Use subqueries in the from clause
(1) The query result is used as a temporary table
Through the previous example, we found that every time we execute a statement, a new table will appear. Then we can treat this table as a temporary table for subsequent query needs.
(2) Example
Display the name, department, salary, and average salary of each employee whose salary is higher than the average salary of his department
Let's first check the average salary of each department. The form is shown in the figure below:
Now we use this table as a new table, and name this table tmp. Next, we can complete our topic in the way of multi-table query.
select ename, emp.deptno, sal, asal from emp,(select deptno, avg(sal) asal from emp group by deptno)o) tmp where emp.deptno = tmp.deptno and sal > asal;
Find the name, salary, department, highest salary of the person with the highest salary in each department
select ename, sal, emp.deptno, msal from emp,(select deptno, max(sal) msal from emp group by deptno)) tmp where emp.deptno = tmp.deptno and emp.sal = msal;
Display the information of each department (department name, number, address) and the number of personnel
6. Merge query
In practical applications, in order to combine the execution results of multiple selects, the set operators union and union all can be used.
(1)union
effect
This operator is used to obtain the union of two result sets. When using this operator, duplicate rows in the result set are automatically removed.
Example: Find out the people whose salary is greater than 2500 or whose position is MANAGER
select ename, sal, job from emp where sal > 2500 union select ename, sal, job from emp where job = 'MANAGER';
(2)union all
effect
This operator is used to obtain the union of two result sets. Duplicate rows in the result set are not removed when this operator is used.
Example: Find out the people whose salary is greater than 25000 or whose position is MANAGER
select ename, sal, job from emp where sal > 2500 union all select ename, sal, job from emp where job = 'MANAGER';