Differences among left join, right join and inner join of sql

Posted by BeastRider on Tue, 04 Jan 2022 04:22:42 +0100

[differences among left join, right join and inner join of sql]

Quoted from https://www.cnblogs.com/pcjim/articles/799302.html)

Left join returns records that include all records in the left table and the same join field in the right table
Right join returns records that include all records in the right table and the same join field in the left table
Inner join returns only rows with equal join fields in two tables

1.left join

/*
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

--------------------------------------------
*/

select * from A
left join B
on A.aID = B.bID;
/*
The results are 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

(Number of rows affected (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)
The insufficient records in table B are NULL

2.right join

--sql The statement is as follows:
select * from A
right join B
on A.aID = B.bID;
/*
The results are 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

(Number of rows affected (5 rows)
*/

Result description:
If you look closely, you will find that, contrary to the result of left join, this time it is based on the right table (B), and the insufficient parts of table A are filled with NULL

3.inner join

--sql The statement is as follows:
select * from A
innerjoin B
on A.aID = B.bID
/*
The results are 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 shown here This shows that the inner join is not based on who. It only displays qualified records

Note:
The LEFT JOIN operation is used to combine the records of the source table in any FROM clause. Use the LEFT JOIN operation to create a left outer join. The left outer join will contain all the records in the two tables starting FROM the first (left), even if there are no records with matching values in the second (right) table.

Syntax: from table1 left join table2 on table1 field1 compopr table2. field2

Note: the table1 and table2 parameters are used to specify the name of the table to combine records.
The field1 and field2 parameters specify the name of the joined field. 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 compr parameter specifies the relational comparison operator: '=', '<', '>', '< =', '> =' or '< >'.
If you want to join a field containing Memo data type or OLE Object data type data in the INNER JOIN operation, an error will occur