MySQL learning - multi-table query (5)

Get into the habit of writing together! This is the 17th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

foreword

In the last article, we learned some multi-table queries. Interested friends can read ( # MySQL learning - multi-table query (4) ).
Let's continue to learn about multi-table queries in MySQL.

SQL99 inner join

Example 1

Query employee's name, department name.

In the previous article, we have written an example of an inner join. Here is another way to write it:

SELECT e.name, d.department_name
FROM employees e INNER JOIN departments d
ON e.'department_id' = d.'department_id'
复制代码

The correct result can also be found by using the above statement. It can be seen that there is an additional 'INNER' in the grammar, which represents an inner join, but in the first writing method, we omit 'INNER'. Correspondingly below, we look at the writing method of outer join.

SQL99 left outer join

Example 2

Query the names and department names of all employees.

SELECT e.name, d.department_name
FROM employees e LEFT OUTER JOIN departments d
ON e.'department_id' = d.'department_id'
复制代码

In this way, the correct result can be found, and the query of the left outer join can be performed by adding the 'LEFT OUTER' left outer join.
Here's another way to write it:

SELECT e.name, d.department_name
FROM employees e LEFT JOIN departments d
ON e.'department_id' = d.'department_id'
复制代码

You can also find out the correct result. It is also possible to omit 'OUTER' here.

SQL99 right outer join

Example three

SELECT e.name, d.department_name
FROM employees e RIGHT JOIN departments d
ON e.'department_id' = d.'department_id'
复制代码

Here you can use 'RIGHT JOIN' for right outer join queries.

SQL99 full outer join

Example four

SELECT e.name, d.department_name
FROM employees e FULL OUTER JOIN departments d
ON e.'department_id' = d.'department_id'
复制代码

We use the 'FULL OUTER JOIN' in the syntax to query, but found that an error was reported because MySQL does not support the 'FULL OUTER JOIN' syntax. How can MySQL do a query full of outer joins? We need to learn the use of the 'UNION' keyword first.

Use of UNION

Combine query results

Using the UNION keyword, multiple SELECT statements can be given and their results combined into a single result set. When merging, the number of columns and data types corresponding to the two tables must be the same and correspond to each other. Use the 'UNION' or 'UNION ALL' keyword to separate each SELECT statement.

Syntax format:

SELECT name FROM employees
UNION [ALL]
SELECT department_id FROM departments
复制代码

UNION operator

The UNION operator returns the union of the result sets of two queries, with duplicate records removed.

UNION ALL operator

The UNION ALL operator returns the union of the result sets of the two queries, without deduplication.

In actual use, the result of deduplication, that is, the UNION operator, is consistent with the case of full outer join. But if both the UNION operator and the UNION ALL operator are in line with the situation, then we'd better use the UNION ALL operator, because the UNION operator has one step to deduplicate, which will affect the efficiency of the query.

Learn here today and continue tomorrow.

Guess you like

Origin juejin.im/post/7087531620021829640