MySQL learning - multi-table query (3)

Get into the habit of writing together! This is the 15th 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 (2) ).
Let's continue to learn about multi-table queries in MySQL.

Multi-table query classification

  • Equijoin VS Non-equijoin
  • Self-connecting vs non-self-connecting
  • Inner join vs outer join

Equijoin

When querying two tables, using an equal sign to join is an equijoin. The examples we queried in the previous article are all equijoins. Not much to say here.

non-equijoin

When querying two tables, using a non-equal sign to join is a non-equivalent join. Let's look at two examples below.

Example 1

Check out pay grades.

SELECT *
FROM job_grades
复制代码
grade_level lowest_sal highest_sal
A 1000 2999
B 3000 5999
C 6000 9999

You can see the three levels of ABC in the salary.

Example 2

Query the employee's name, salary and corresponding salary grade.

SELECT e.name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.'salary' BETWEEN j.'lowest_sal' AND j.'highest_sal'
复制代码
name salary grade_level
bing 2000 A
dun 8000 C
dong 5600 B

An employee's salary grade can be found out using BETWEEN AND.

You can also use the following notation:

SELECT e.name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.'salary' >= j.'lowest_sal' AND e.'salary' <= j.'highest_sal'
复制代码

The same result can also be found.

non-self-connection

The joins of different tables in the above example are all non-self joins.

self-connection

The connection between oneself and oneself is called self-connection.

Example three

Query employee id and name and their manager's id and name.

SELECT emp.employee_id, emp.name, mgr.employee_id, mgr.name
FROM employees emp, employees mgr
WHERE emp.'manager_id' mgr.'employee_id'
复制代码

In this way, the result can be found out. This method is called self-connection.

inner join

Merges rows from two or more tables that have the same column, and the result set does not contain rows where one table does not match the other. Include only matching lines.

outer join

Merges rows from two or more tables with the same column, except that the result set contains rows from one table that match the other. Also includes rows that do not match in either the left or right table.

Classification of outer joins

  • left outer join
  • right outer join
  • full outer join

left outer join

In the join process of two tables, in addition to returning rows that satisfy the join condition, the rows in the left table that do not satisfy the condition are also returned. This join is called a left outer join.

right outer join

In the join process of two tables, in addition to returning the rows that satisfy the join condition, the rows in the right table that do not satisfy the condition are also returned. This join is called a right outer join.

Learn here today and continue tomorrow.

Guess you like

Origin juejin.im/post/7086816850352275492