[Database] The difference between left join, right join and inner join in SQL

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

left join (left join) returns including all records in the left table and the join field in the right table is equal to the record

right join (right join) returns including all the records in the right table and the records in the left table where the join field is equal

inner join (equivalent join) only returns rows with equal join fields in 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.left join

sql语句如下:
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

(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 the left join is based on the left table.
Change 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).

Insufficient records in table B are all NULL.

2.right join

The sql 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

(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 the left join. This time it is based on the right table (B), and the lack of A table is filled with NULL .


3.inner join

The sql 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 explanation:
Obviously, only the records with A.aID = B.bID are shown here. This shows that inner join is not based on who, it only shows the records that meet the conditions.

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 outer join on the left 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.

语法: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 to which the records will be combined.
The field1, field2 parameters specify the names of the connected fields. 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 the relational comparison operator: "=", "<", ">", "<=", ">=" or "<>".
If you want to join fields containing Memo data type or OLE Object data type data in the INNER JOIN operation, an error will occur.

Guess you like

Origin blog.csdn.net/m0_37882192/article/details/109961198