6. Table connection

1. Introduction to
table connection Table connection is to make associations between tables through certain connection conditions between multiple tables, and then to obtain data from multiple tables. There are three types of table connection, internal connection, external connection (left external connection, right external connection and full connection) and self-connection.

2. Inner connection
Only connect matching rows.
Insert picture description here

select a.field, b.field 
from tableName1 as a inner join tableNam2 as b
on a.field = b.field

As shown in the figure below, query the student's academic performance.
Insert picture description here

3. Left outer join
contains all rows of the left table (regardless of whether there are matching rows in the right table), and all matching rows in the right table.
Insert picture description here

select a.field, b.field 
from tableName1 as a left join tableName2 as b
on a.field = b.field

As shown in the figure below, query the student's academic performance.
Insert picture description here

4. Right outer join
contains all rows of the right table (regardless of whether there are matching rows in the left table), and all matching rows in the left table.
Insert picture description here

select a.field, b.field 
from tableName1 as a right join tableName2 as b
on a.field = b.field

As shown in the figure below, query the student's academic performance.
Insert picture description here

5. Full join (not supported in MySQL)
contains all rows of the left and right tables, regardless of whether there is a matching row in the other table.
Insert picture description here

select a.field, b.field 
from tableName1 as a full outer join tableName2 as b
on a.field = b.field

6. Self-connection
Self-connection is a special table connection, which means that the connected tables are physically the same as one table, but logically there are multiple tables. Self-joins are usually used for data in tables that have a hierarchical structure, such as area tables, menu tables, and commodity classification tables.

select tableName1.field, tableName2.field 
from tableName1, tableName2
where tableName1.field = tableName2.field;

As shown in the figure below, query the student's academic performance.
Insert picture description here

Guess you like

Origin blog.csdn.net/Jgx1214/article/details/107496073