The difference between left join, right join, and inner join of SQL

The difference between left join, right join and inner join of sql

left join (left join) returns including all records in the left table and records with equal join fields in the right table
right join (right join) returns including all records in the right table and Records with equal join fields in the left table
inner join (equivalent join) only returns rows with equal join fields in the two 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 as follows:
bID bName
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408

--------------------------------------- -
1. The left join
sql statement is as follows:
select * from A
left join B
ON A.AID = B.BID

results are as follows:
AID ANUM BID BNAME
1 A20050111 1 2006032401
2 A20050112 2 2006032402
3 A2005013 3 2006032403
4 A20050114 4 2006032404
5 A20050115 NULL NULL

(5 rows of number of rows)
:
Left Join It 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, the records of the left table (A) will all be displayed. , and the right table (B) will only display the records that meet the search conditions (in the example: A.aID = B.bID).
The places where the records in table B are insufficient are all 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 second is based on the right table (B), and the shortage of table A is filled with NULL. ------------------------------ --------------- 3. The inner join sql 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 the records with A.aID = B.bID are displayed here. This shows that the inner join is not based on who, it only displays the records that meet the conditions.

Guess you like

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