Home > Database > SQL > body text

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

青灯夜游
Release: 2020-07-16 11:31:49
Original
64342 people have browsed it

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
Copy after login

Only one record can be obtained:

小李  老师
Copy after login

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
Copy after login
Three records:

  小王  null
 
  小李  老师
 
  小刘  null
Copy after login
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
Copy after login

Two Records:

  小李  老师
 
  null  程序员
Copy after login
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
Copy after login

Four pieces of data

  小王  null
 
  小李  老师
 
  小刘  null
 
  null  程序员
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!