Table of contents
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 SELECT
which fields are selected.
SELECT <select_list>
FROM TableA A
LEFT JOIN TableB B
ON A.Key = B.Key
SELECT
Is the field representing the selection to be returned (i.e. which column to return);FORM
It means which table to select from. Here the left join will form a temporary table and select from the temporary table;TableA A
It can also be written asTableA as A
, that is, it is givenTableA
a different namea
. The alias is given here so that it can be abbreviated later, so it does not need to be writtenON TableA.A = TableB.B
;LEFT JOIN
Indicates merging the sumTableA
intoTableB
a temporary table ,on
indicating 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/
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 table
null
. 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 displayednull
; - 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
personId
join the equal records in the two tables, retain the rest in the left table, and fill the remaining positions in the right tablenull
.
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 null
the 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