table of Contents
1. SQL JOIN
a brief introdction
The SQL JOIN clause is based on common fields between tables, and joining rows from multiple tables is to combine them.
classification
INNER JOIN: If there is at least one match in the table, the row is returned.
LEFT JOIN: Even if there is no match in the right table, all rows are returned from the left table.
RIGHT JOIN: Even if there is no match in the left table, all rows are returned from the right table.
FULL JOIN: As long as there is a match in one table, the row is returned.
2. INNER JOIN
database
Use the following database to explain:
Two tables, table_1, table_2.
As follows: table_1:
id | Name |
---|---|
1 | |
2 | Taobao |
3 | |
4 |
table_2:
id | Address |
---|---|
1 | United States |
3 | China |
5 | China |
6 | United States |
grammar
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
或者
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Icon
From this figure, we can see that the data taken out by INNER JOIN is the data that the two tables intersect.
Statement example
select * from table_1
inner join table_2
on table_1.id=table_2.id
The output of executing the above statement is as follows:
id | Name | Address |
---|---|---|
1 | United States | |
3 | China |
3. LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table (table_1), even if there is no match in the right table (table_2). If there is no match in the right table, the result is NULL.
grammar
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Icon
Statement example
select * from table_1
left join table_2
on table_1.id=table_2.id
The output of executing the above statement is as follows:
id | Name | Address |
---|---|---|
1 | United States | |
2 | Taobao | null |
3 | China | |
4 | null |
4. RIGHT JOIN
The RIGHT JOIN keyword will return all rows from the right table (table_2), even if there is no match in the left table (table_1). If there is no match in the left table, the result is NULL.
grammar
SELECT column_name(s)
FROM table_1
RIGHT JOIN table_2
ON table_1.column_name=table_2.column_name;
Icon
Statement example
select * from table_1
right join table_2
on table_1.id=table_2.id
The output of executing the above statement is as follows:
id | Name | Address |
---|---|---|
1 | United States | |
3 | China | |
5 | null | China |
6 | null | United States |
5. FULL JOIN
FULL JOIN will return all rows from the left and right tables. If the data row of one table does not have a matching row in the other table, then the opposite data is replaced with NULL.
grammar
select column_name(s)
from table 1
FULL OUTER JOIN table 2
ON table 1.column_name=table 2.column_name
Icon
Statement example
select * from table_1
full outer join table_2
on table_1.id=table_2.id
The output of executing the above SQL is as follows:
id | name | address |
---|---|---|
1 | United States | |
2 | Taobao | null |
3 | China | |
4 | null | |
5 | null | China |
6 | null | United States |
The picture comes from the rookie tutorial.