Xiaobai takes you to understand the outer join and inner join in SQL

Database table data:

	A表          
  id   name  
 
  1  小王
 
  2  小李
 
  3  小刘
 
  B表
 
  id  A_id  job
 
  1  2    老师
 
  2  4    程序员

1. Left join, return records that include all records in the left table and the join fields in the right table are equal, and replace the field values ​​that do not exist in the right table with null.
For example:

select a.name,b.job from A a  left join B b on a.id=b.A_id

result:

	小王  null
 
  小李  老师
 
  小刘  null

2. Right join, return including all records in the right table and the records with the same joining field in the left table, and replace the field values ​​that do not exist in the left table with null.
For example:

select a.name,b.job from A a  right join B b on a.id=b.A_id

result:

	小李  老师
 
  null  程序员

3. Inner join, only return rows with equal join fields in the two tables.
For example:

select a.name,b.job from A a  inner join B b on a.id=b.A_id

result:

	小李  老师

4. Full outer join, return all the records in the left and right tables and the records with the same connection fields in the left and right tables, replace with null if there is no field value on both sides.
For example:

select a.name,b.job from A a  full join B b on a.id=b.A_id

result:

	小王  null
  小李  老师
 
  小刘  null
 
  null  程序员

Guess you like

Origin blog.csdn.net/weixin_44703894/article/details/114382240