Illustration of left join, right join, inner join and full join (for beginners)

1. Seven connection situations

2. Left join

The full name of left join is left outer join, and left join will return the data in the left table 每一条记录(即每一行数据). Note: This is not to say 每一个数据, it depends on SELECTwhich fields are selected.

SELECT <select_list>
FROM TableA A
LEFT JOIN TableB B
ON A.Key = B.Key
  • SELECTIs the field representing the selection to be returned (i.e. which column to return);
  • FORMIt means which table to select from. Here the left join will form a temporary table and select from the temporary table;
  • TableA AIt can also be written as TableA as A, that is, it is given TableAa different name a. The alias is given here so that it can be abbreviated later, so it does not need to be written ON TableA.A = TableB.B;
  • LEFT JOINIndicates merging the sum TableAinto TableBa temporary table , onindicating the conditions for the connection . Here it means connecting the records with equal keysON A.Key = B.Key in the two tables . When the temporary table is generated for the records with unequal keys, the records in the left table will be retained, and the remaining positions in the right table will be retained. Just use padding.null

Specific example: Likou Database Topic 175. Combining two tables

Link: https://leetcode.cn/problems/combine-two-tables/description/

Please add image description
Please add image description

Answer

select firstName, lastName, city, state
from Person p left join Address a
on p.Personid = a.Personid
  • on is the connection condition when generating the temporary tablenull . If the left and right tables meet this condition, they will be connected directly. If they do not meet this condition, the right table will fill in the records that do not meet the conditions . But no matter what, the records in the left table will be returned in the end. If the right table meets the conditions, the corresponding data will be displayed. If it does not meet the conditions, it will be displayed null;
  • The conditions behind where are filtered after the temporary table is generated . Those that do not meet the where conditions will be filtered out.
  • The Person table has 2 rows, and the Address table has 2 rows. 2 x 2 = 4. A total of 4 rows can be formed in 4 situations.

1. The connection condition is: on Person.PersonId = Address.personId

  • This join condition means to personIdjoin the equal records in the two tables, retain the rest in the left table, and fill the remaining positions in the right table null.

Please add image description

2. The connection condition is: on Person.PersonId != Address.personId

This connection condition means to connect the records with unequal personId in the two tables, keep the rest in the left table, and fill the remaining positions in the right table null.

3. Left join does not include inner join

SELECT <select_list>
FROM TableA A
LEFT JOIN TableB B
ON A.Key = B.Key
WHERE B.KEY IS NULL

The B.Key requirement here is to get the part without B in nullthe left connection .临时表

4. Right connection

Right join is similar to left join.

SELECT <select_list>
FROM TableA A
RIGHT JOIN TableB B
ON A.Key = B.Key

5. Right join does not include inner join

SELECT <select_list>
FROM TableA A
RIGHT JOIN TableB B
ON A.Key = B.Key
WHERE A.Key IS NULL

5. Inner connection

SELECT <select_list>
FROM TableA A 
INNER  JOIN TableB B
ON A.key = B.key
  • If you just write it simply join, it will default to内连接

6. Full connection

SELECT <select_list>
FROM TableA A
FULL OUTER JOIN TableB B
ON A.Key = B.Key

7. Full join does not include inner join

SELECT <select_list>
FROM TableA A
FULL OUTER JOIN TableB B
ON A.Key = B.Key
WHERE A.Key IS NULL
OR B.Key IS NULL

8. Reference materials

  1. Illustration: The most detailed database left join, right join, inner join, and full join in history!
  2. Illustration of MySQL inner join, outer join, left join, right join, full join... too many
  3. The difference between the restriction conditions in left join and where

Guess you like

Origin blog.csdn.net/e2788666/article/details/131259484