[MySQL] Compound query

foreword

        hello~ welcome to my MySQL notes series.

        In the actual query of the table in the database, we often face not one object, but multiple objects . For example, the course number marked on a grade sheet is not marked with the course name. If we want to know the name of the course through the grade sheet, we must query the class table.

        So in this note, I record the multi-table query, nested query, and internal and external connection problems under MySQL.

        Note that the test table used in this article is the classic Oracle 9i test table : scott_data.sql (use the source to restore the database: scott-(dept department table, emp employee table, salgrade salary grade table))

My last mysql note:

[MySQL] built-in functions

Table of contents

1. Multi-table query

Cartesian product

-Display employee name, employee salary and department name

-Display the department name, employee name and salary of department number 10

-Display the name, salary, and salary level of each employee

Multi-table query process

2. Self-connection

-Display the number and name of the superior leader of the employee FORD

3. Sub query

1. Single row subquery

2. Multi-row subqueries

-in

-all

-any

3. Multi-column subquery

4. Correlated or uncorrelated subqueries

5. Use subqueries in from

6. Set query

union

Four, internal and external connections

1. Inner join

2. Outer join

left outer join

right outer join

full outer join


1. Multi-table query

        If you want to understand multi-table queries, there should be connections between different tables. For example, a foreign key: a certain attribute in the secondary table is a unique key constraint from the main table (primary key or unique key-unique attribute).

        We can effectively connect through foreign keys . But before it works, we can combine the two tables simply and violently :

Cartesian product

        The simple and violent connection of two tables is the Cartesian product . That is to say, each record of one table is combined with each record of another table , so the total number of records is the product of the numbers of the two tables.

select ... from table1 [, table2...];

        For example, we connect emp employee table and dept department table with Cartesian product:

        Therefore, the essence of Cartesian product is the exhaustion of data , which can be similar to the usual multiple violent traversal. 

        So now that they are all connected into one table, is the so-called multi-table query converted to the previous single-table query?

-Display employee name, employee salary and department name

        Because the name of the department is required, the emp table and the dept table need to be associated.

        First of all, we can naturally connect the two tables through the Cartesian product. However, there will be a lot of invalid data in the direct connection, because the deptno of the emp table does not correspond to the deptno of the dept table. At this time, you can use the where filter conditions to correspond. The filtered merged table is a valid table, and the data can be queried.

select ename, sal, dname from emp, dept where emp.deptno=dept.deptno;

        It should be noted that because two tables are merged, if there are duplicate attribute names, the table name must be specified or renamed .

-Display the department name, employee name and salary of department number 10

        Because the department name whose department number is 10 needs to be displayed, the emp table and the dept table need to be connected.

select emp.deptno, dept.dname, ename, sal
from emp, dept 
where emp.deptno=dept.deptno and emp.deptno=10;

-Display the name, salary, and salary level of each employee

        Because there is a salary level, then the emp table and the salgrade table are used for connection screening.

        This screening needs to match sal salaries according to the restrictions in the salgrade table, so as to achieve the purpose of multi-table query.

select ename, sal, grade from emp, salgrade where losal <= sal and sal <= hisal;
--或者:select ename, sal, grade from emp, salgrade where sal between losal and hisal;

Multi-table query process

        To sum up, our idea of ​​solving multi-table query is as follows:

1. Read the questions first to determine which tables are related.
2. Combine to form a table.
3. Think of multi-table query as a query of one table.

2. Self-connection

        Since it is a multi-table query, can the multiple tables be themselves?

        In some scenarios, we need to connect ourselves to achieve what we are looking for. However, in order to distinguish between different tables, self-join needs to be renamed.

select ... from table [as] t1 [, table [as] t2...];

-Display the number and name of the superior leader of the employee FORD

        Because the number of the superior leader is the number of the employee in this table. So if you want to find the superior leader of the corresponding employee, you can do a self-connection to search:

select t1.ename 员工, t1.mgr 领导编号, t2.ename 领导
from emp t1, emp t2
where t1.mgr=t2.empno and t1.ename='FORD';

         Of course, if we don't want to optimize the lookup of the connection between the two tables, we can nest the query to find it:

select empno 领导编号, ename 领导 
from emp
where empno=(select mgr from emp where ename='FORD');

        Nested queries are what we're going to call parent-child queries right away.

3. Sub query

        Subquery means that the select statement is nested in other sql select statements. At this time, the outer select is the parent query, and the inner select is the subquery.

        For subqueries, nesting can be continued, and the execution sequence is similar to a recursive function (stack). And whether the subquery can be divided into correlated subquery and uncorrelated subquery depending on the table of the parent query ; the results returned by the subquery can also be divided into single-row results and multi-row results . And the columns of each record can also be multi-column or single-column .

        And the result returned by the select statement can also form a Cartesian product with the table . Usually, in order to relieve the two tables from being directly merged together, the pressure on where is relieved.

select ...
from table1, [(select ...) [as] s1, ...]
where expression(select ...), ...
[group by column] [having ...]
[order by column] [limit ...];

         Below we use the test table to test the above conclusions.

1. Single row subquery

        The value returned by a single-row subquery is only the existence of one record (filtering one record). At this time, = or in can be used in the where expression to judge.

-Show employees in the same department as SMITH

select ename, deptno from emp where deptno=(select deptno from emp where ename='SMITH');

 

2. Multi-row subqueries

        Because the result returned by the multi-row subquery is multiple records. Then in the where expression in the parent query, you need to use in, all, any and other keywords to query, you can't just use =.

-in

        The subquery is a single-column result, and the where condition of the parent query is a subset of the result .

        -Query the name, position, salary, and department number of the employee who has the same job position as department 10, but does not include 10's own

select ename, job, sal, deptno
from emp where job in (select distinct job from emp where deptno=10)
and deptno <> 10;

         in is generally used to query whether there is the same result as the subquery result, and if it exists, it will be filtered out. (in can also replace the = symbol)

-all

        all means all. That is to say, the conditions in the where expression of the parent query must be satisfied, either greater than the value of all subquery results, or less than all .

        That is, a > all(b) is equivalent to: a > max(b); a < all(b) is equivalent to a < min(b);

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

-any

        any means any. That is to say, the conditions in the where expression of the parent query must be satisfied or cannot be greater than the entire value of the subquery results, or cannot be less than the entire value .

        Translate the above: a>any(b) -> a>min(b); a<any(b)->a<max(b);

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

3. Multi-column subquery

        Note that the where expression conditions of the parent query above are all filtered by a single attribute. So what if there are multiple attributes? We need to add multiple attributes to the conditions for filtering.

        -Query all employees who have the same department and position as SMITH, excluding SMITH himself

select ename
from emp where (deptno, job)=(select deptno, job from emp where ename='SMITH')
and ename <> 'SMITH';

 

         Of course there is no problem with in. It's just that a single attribute has been replaced with multiple attributes.

4. Correlated or uncorrelated subqueries

        The query conditions of the correlated subquery depend on the parent query. First, the first tuple of the table in the outer query is taken, and the inner query is processed according to its attribute value related to the inner query. If the return value of the WHERE clause is true , then Take this tuple and put it into the result table; then take the next tuple in the outer table, and repeat this process until all the outer tables are checked.

        The query conditions of an uncorrelated subquery do not depend on the parent query, and are processed layer by layer from the inside out. That is, each sub-query is solved before the upper-level query is processed, and the result of the sub-query is used to establish the search conditions of its parent query.

        None of the select statements we've written before are correlated subqueries. Here is an example of a correlated subquery:

        -Query all employees in the emp table whose salary is greater than the average salary of their current department .

- Correlated subqueries:

select ename, sal, deptno from emp e1 
where sal > (select avg(sal) from emp e2 where e1.deptno=e2.deptno)
order by deptno;

- Uncorrelated subqueries:

select ename, sal, emp.deptno
from emp, (select deptno, avg(sal) 平均工资 from emp group by deptno) s1
where emp.deptno=s1.deptno and sal > 平均工资 order by deptno;

        It can be seen that correlated subqueries are very elegant in specific occasions, and there is no need to perform join queries.

        In fact, the second uncorrelated subquery is used to merge tables by using select in from to perform Cartesian product operation.

5. Use subqueries in from

        Subquery statements can not only appear in the where clause, but can also appear in the from clause as a temporary table for Cartesian product with other tables.

        Use this trick to reduce the stress of where filtering.

-Display name,department,salary,average salary of every employee whose salary is higher than average salary of own department

select ename, deptno, sal, format(asal, 2) 平均工资
from emp, (select deptno dtno, avg(sal) asal from emp group by dtno) tmp
where deptno=dtno and sal > asal
order by deptno;

 

- Find the name, salary, department, highest salary of the person with the highest salary in each department

select emp.ename, emp.sal, emp.deptno, 最高工资
from emp, (select deptno, max(sal) 最高工资 from emp group by deptno) tmp
where emp.deptno=tmp.deptno and sal=最高工资;

 

-Display the information of each department (department name, number, address) and the number of personnel

select dept.deptno, dname, loc, 人员数量 
from dept, (select deptno, count(*) 人员数量 from emp group by deptno) tmp
where dept.deptno=tmp.deptno;

 

6. Set query

        In addition, if the attributes of the two tables are the same (the columns and data types are the same) , we can perform set query operations.

        Set query is divided into three types: union operation: union, intersection operation: intersect, difference operation: except ;

        For the union operation, it is merged together, and automatically excludes duplicates (just keep and add all), the intersection operation is the same part of the two query results, and the difference operation is the first result minus the second same result .

select ...
[集合操作]
select ...

        It should be noted that the old version of mysql does not support the intersection operation, sum operation and difference operation. SQL Server is supported. 

        Use the following examples to deepen understanding:

union

-Find out those whose salary is greater than 2500 or whose position is MANAGER

        The first is to automatically exclude duplicates

select ename, sal, job from emp where sal>2500
union
select ename, sal, job from emp where job='MANAGER';

 

         Do not exclude duplicates

select ename, sal, job from emp where sal>2500
union all
select ename, sal, job from emp where job='MANAGER';

 

Four, internal and external connections

1. Inner join

        The inner join is to use the where clause to filter the Cartesian product formed by the two tables. The queries we learned earlier are all inner joins
, and they are also the most used join queries in the development process. If there is NULL in a record in a table, the connection will not be displayed.

        For example, we create two tables in our test database test2_db; a student name and student number-student table. A student number and the corresponding score-grade sheet. The creation statement is as follows:

        Create the student table Stu:

create table Stu(
    name varchar(11) not null,
    sno int(10) zerofill auto_increment,
);

        Create a grade table Grade:

create table Grade(
sno int(10) unsigned, 
grade float(4, 1),
);

         Now simply insert the following data into the two tables:

-- Stu
insert into Stu values ('张三', 2100000000), ('李四', 2100000001), ('王五', 2100000002), (NULL, 2100000003), ('小明', NULL);
-- Grade
insert into Grade values (2100000000, 89.5), (2100000001, NULL), (2100000002, 45.5), (2100000003, 97), (NULL, 99);

        We follow the inner link, and only the matched records will be displayed (the default effect was without these keywords before)

select ... from table1 inner join table2... on... where ...;

         Usually you can use on to express matching restrictions on table connections, and where to filter records.

        For example, we match the corresponding students and grades with the above table:

select * from Stu inner join Grade on Stu.sno=Grade.sno;

2. Outer join

        Outer join means that two or more tables that do not match can be added to it. Corresponding attributes that do not match will be filled in. Outer joins are divided into left outer joins and right outer joins. A full outer join can be implemented using a collection query union.

left outer join

        The left outer join will join the records that do not match in the left table1 table.

select ... from table1 left join table2... on... where ...;

        We use the left outer join to connect the above tables Stu and Grade to see if the sno is NULL and the sname is Xiao Ming will be displayed:

select * from Stu left join Grade on Stu.sno=Grade.sno;

 

 

right outer join

        The right outer join will join the records that do not match in the table2 table on the right.

select ... from table1 right join table2... on... where ...;

         We use the right outer join to connect the above tables Stu and Grade to see if the sno is NULL and the grade is 99 will be displayed:

select * from Stu right join Grade on Stu.sno=Grade.sno;

 

full outer join

        The full outer join is to display the unmatched records in the left and right tables, and fill in NULL for the missing attributes. Combined queries can be used in mysql to achieve:

select * from Stu left join Grade on Stu.sno=Grade.sno
union
select * from Stu right join Grade on Stu.sno=Grade.sno;

 

Guess you like

Origin blog.csdn.net/weixin_61508423/article/details/130356197