left join (left join) right join (right join) inner join (equivalent join) difference

left join (left join) Returns records including all records in the left table and the join fields in the right table are equal 
right join (right join) Returns records including all records in the right table and the join fields in the left table are equal
inner join (etc. value join) returns only the rows with equal join fields in both tables

Examples are as follows: 
--------------------------------------------
Table A records As follows:
aID aNum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115

Table B records the following:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408

--------------------------------------------
1.left join
sql语句如下: 
select * from A
left join B 
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL

(The number of rows affected is 5 rows)
Result description: left join is based on the records of table A, A can be regarded as the left table, B can be regarded as the right table, and left join is based on the left
table.
In other words, all the records in the left table (A) will be displayed, while the right table (B) will only display the records that meet the search criteria (in the example: A.aID = B.bID).
Where there are insufficient records in table B All are NULL.
--------------------------------------------
2. The right join
sql statement is as follows: 
select * from A
right join B 
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 2006032408

(The number of rows affected is 5 rows)
Result description:
If you look closely, you will find that it is just the opposite of the result of left join. This time it is based on the right table (B), and the shortage of table A is filled with NULL. .--------------------------------------------
3.inner
join
sql The statement is as follows: 
select * from A
innerjoin B 
on A.aID = B.bID

The result is as follows:
aID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404

Result description:
Obviously, only records with A.aID = B.bID are displayed here. This shows that inner join is not based on who, it only displays records that meet the conditions.
---------- ----------------------------------
NOTE: 
The LEFT JOIN operation is used in any FROM clause, combining The records of the source table. Use the LEFT JOIN operation to create a left outer join. A left outer join will include all records from both tables starting from the first (left) table, even if there are no matching records in the second (right) table.

语法:FROM table1 LEFT JOIN table2 ON table1.field1 compopr table2.field2

Description: The table1, table2 parameters are used to specify the name of the table in which the records are to be combined.
The field1, field2 parameters specify the names of the fields to be joined. And these fields must have the same data type and contain the same type of data, but they do not need to have the same name.
The compopr parameter specifies a relational comparison operator: "=", "<", ">", "<=", ">=" or "<>".
An error will occur if fields containing Memo data type or OLE Object data type data are to be joined in an INNER JOIN operation. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253759&siteId=291194637