Detailed explanation of join statement in SQL

1.inner join (inner join)

Only matching rows are returned.
inner join
select * from table_a a inner join table_b b on a.name = b.name

2.left join (left outer join)

Returns all the data in the left table and the rows in the right table that meet the on condition. If the rows in the left table do not have matching data in the right table, then the data corresponding to the right table in this row will be null.
left outer join
select * from table_a a left join table_b b on a.name = b.name

3.right join (right outer join)

Returns all the rows in the right table and the rows in the left table that meet the on condition. If the rows in the right table do not match the left table, then the corresponding data in the left table in this row is null.
right outer join
select * form table_a a right join table_b b on a.name = b.name

4.left join excluding inner join (left join-inner join)

That is to add a where condition (b.name is null) to the left join query to filter out the intersection of tables A and B.
left join will return all the left table, the matching ones in the right table are returned, and the unmatched null values ​​are filled in. This query just filters out the null records in the right table select *

Insert image description here
from table_a a left join table_b b on a.name = b .name where b.name is null

5.right join excluding inner join(right join-inner join)

That is to add a where condition (a.name is null) to the right join query to filter out the intersection of tables A and B.
right join will return all the right table, the matching ones in the left table are returned, and the unmatched ones are filled with null. This query just filters out the null records in the left table

Insert image description here
select * from table_a a right join table_b b on a.name = b. name where a.name is null

**

Note that MySQL does not support full outer join

**

6.full outer join (outer join)

It will return all the rows of the left table and the right table. If there is no data in the corresponding table, it will be filled with null.
Insert image description here
Since the mysql database does not support full outer join, you need to use left join + left join instead of select
* from table_a a left join table_b b on a.name = b.name
union
select * from table_a a right join table_b b on a. name = b.name

7.full outer join excluding inner join (outer join-inner join)

Insert image description here
select * from table_a a left join table_b b on a.name = b.name where b.name is null
union
select * form table_a a right join tablb_b b on a.name = b.name where a.name is null

Guess you like

Origin blog.csdn.net/qq_42182034/article/details/107535320