Home >Database >SQL >What is the difference between left join, right join, inner join and full outer join?

What is the difference between left join, right join, inner join and full outer join?

青灯夜游
青灯夜游Original
2020-07-16 11:31:4964702browse

Difference: left join returns all records in the left table that are equal to the join field in the right table; right join returns all records in the right table and records that are equal to the join field in the left table; inner join Only return rows with equal join fields in the two tables; full outer joins return all records in the left and right tables and records with equal join fields in the left and right tables.

What is the difference between left join, right join, inner join and full outer join?

left join (left join, left outer join): Returns all records in the left table that are equal to the join fields in the right table.

right join (right join, right outer join): Returns all records in the right table that are equal to the join fields in the left table.

inner join (equivalent join or inner join): Only return rows with equal join fields in the two tables.

full join (full outer join): Returns all records in the left and right tables that are equal to the join fields in the left and right tables.

For example:

A table

id Name 

1 Xiao Wang

2 Xiao Li

3

#Inner join: (Only matching rows from 2 tables can be displayed)

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

Only one record can be obtained:

小李  老师

Left join: (The one on the left The table is not restricted)

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

  小王  null
 
  小李  老师
 
  小刘  null
Right join: (The table on the right is not restricted)

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

Two Records:

  小李  老师
 
  null  程序员
Full external join: (no restrictions on the left and right tables)

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

Four pieces of data

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

Note: In SQL, outer joins include left join and right join, full outer join, and equal join (inner join), also called inner joins.

Related recommendations: "SQL Tutorial

"

The above is the detailed content of What is the difference between left join, right join, inner join and full outer join?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn