Operation of multi-table query in Chapter 2 of Mysql


Because when directly connecting multiple tables, the Cartesian product problem leads to the problem of multi-table joint query. Multi-table queries are basically divided into three categories, outer join and inner join, equivalent and non-equivalent, self-join and non-self-join

A concept of outer join and inner join

These connection relations, in the final analysis, are intersection and complement operations of sets
. Finding the common part of two tables is called an inner join, which is equivalent to an intersection.
Finding the common part of two tables plus the left is called a left outer join.
If it is a left outer join , the table on the left of the join condition is also called the main table, and the table on the right is called the slave table.
If it is a right outer join, the table on the right of the join condition is called the master table, and the table on the left is called the slave table

SQL99 syntax implementation defaults to inner join

SELECT last_name,department_name
FROM employees e INNER JOIN departments d
on e.department_id=d.department_id

The result is displayed as 102 pieces of data:
insert image description here

The sql99 syntax realizes the left outer join, and finds out employees without departments

SELECT last_name,department_name
FROM employees e LEFT  OUTER JOIN departments d
on e.department_id=d.department_id;

The result is 103 pieces of data,
which is equivalent to the left is all the information of the employee table, and the right is part of the information of the department table.
The intersection of the employee table and the department table, e and d, and the information that the department table is NULL but the employee table has people
insert image description here

insert image description here

The sql99 syntax realizes the right outer join, and finds out the department that has no one

SELECT last_name,department_name
FROM employees e RIGHT  OUTER JOIN departments d
on e.department_id=d.department_id;

The result can be seen in the picture, there are 119 pieces of information,
insert image description here
that is, the key is the department table on the right, so it is called, right outer join, the first thing to find out is all the departments

insert image description here

sql99 syntax realizes full outer join, mysql does not support writing like this

SELECT  last_name,department_name
FROM employees e
FULL OUTER departments d
ON e.department_id=d.department_id 

If you want to achieve the effect of full outer join in mysql, it is recommended to use the union keyword

Union keyword, returns a union, similar to A and B, will perform de-duplicated retrieval operation
union all returns union plus intersection, the advantage is that it is more efficient

  • Query employee information whose department number is >90 or whose mailbox contains a
SELECT * FROM employees WHERE email LIKE '%a%' 
UNION SELECT * FROM employees WHERE department_id>90;
  • Query all department numbers and all employee names, need to remove duplicates
#查找所有的员工名字,以及所有的部门
SELECT last_name,department_name
FROM employees e LEFT  OUTER JOIN departments d
on e.department_id=d.department_id
UNION 
SELECT last_name,department_name
FROM employees e RIGHT  OUTER JOIN departments d
on e.department_id=d.department_id;

The results show that there are 118 pieces of information
insert image description here
. There is a problem here, that is, the data of the right outer connection is actually more than the number of the union after deduplication. Let’s solve it later.

  • Query all department numbers and all employee names, no need to remove duplicate
    results 222 pieces of information
SELECT last_name,department_name
FROM employees e LEFT  OUTER JOIN departments d
on e.department_id=d.department_id
UNION ALL
SELECT last_name,department_name
FROM employees e RIGHT  OUTER JOIN departments d
on e.department_id=d.department_id;

Two concepts of self-join and non-self-join

  • Self-connection refers to self-referencing in multi-table queries to
    query employee id, employee name, manager ID and name
# 查询员工id,员工姓名,管理者的ID和姓名
SELECT emp.employee_id,emp.last_name,mgr.employee_id 经理工号,mgr.last_name 经理名字
FROM employees emp,employees mgr
WHERE emp.manager_id=mgr.employee_id;
  • Non-self-join, common multi-table query
SELECT last_name,department_name
FROM employees e INNER JOIN departments d
on e.department_id=d.department_id
 

The concept of triple equivalent connection and non-equivalent connection

1.1 Equivalent connection

Equivalent join is also called display inner join. When performing multi-table joint query, the "=" equal sign is used to connect the values ​​corresponding to the corresponding fields between multiple tables, and the result will have duplicate columns. It means that if you perform an equivalence join operation on multiple tables, the prerequisite is that the multiple tables must have the same field name. , let's say the primary key of one table is the foreign key of another table

SELECT last_name,department_name
FROM employees e INNER JOIN departments d
on e.department_id=d.department_id

1.1 Non-equivalent connection

The biggest feature of the non-equivalent connection is that the relationship in the connection condition is a non-equivalent relationship.
insert image description here
Query an employee's name, salary and grade

SELECT last_name,salary,grade_level
FROM employees e,job_grades j
WHERE e.salary BETWEEN j.lowest_sal and j.highest_sal;

Forty-seven kinds of JOIN realization

insert image description here

1 Inner connection A∩B

# 内连接 A∩B
SELECT e.employee_id,e.last_name,d.department_name
FROM employees e
JOIN departments d
ON  e.department_id=d.department_id;

2 left outer join

SELECT e.employee_id,e.last_name,d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id;

3 right outer join

# 右外连接,右边的项目作为主表,左边的是从表
SELECT e.employee_id,e.last_name,d.department_name
FROM employees e
RIGHT JOIN departments d 
ON e.department_id =d.department_id;

4 A - A∩B

# A - A∩B 有名字,没有部门
SELECT e.employee_id ,e.last_name,d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id
WHERE d.department_id
IS NULL;

5 B - A∩B

SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id
IS NULL;

6 full outer joins

Left outer join on B - A∩B

SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id
IS NULL
union all
SELECT e.employee_id,e.last_name,d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id;

7 full outer join - inner join

SELECT employee_id,last_name,department_name
FROM employees e
RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id
IS NULL
union all
SELECT e.employee_id ,e.last_name,d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id=d.department_id
WHERE d.department_id
IS NULL;

Five natural join and USING

Automatically query all the same fields in the two joined tables, and then perform an equivalence join

SELECT employee_id,last_name,department_name 
FROM employees e NATURAL JOIN departments d;

USING is a bit more optimized than natural join

SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d 
USING (department_id);

Multi-table queries need to be restricted, too many are equivalent to multiple for loops, consuming resources

Guess you like

Origin blog.csdn.net/CNMBZY/article/details/130450825