SQL left join and right join principle and example analysis

SQL left join and right join principle and example analysis

There are two tables, and the records in table A may not exist in table B.

  • Left join: focus on the left side, if there is no right side, it will be empty.
  • Right connection: focus on the right side, if there is no left side, it will be empty.
  • Inner Join: Returns the intersection

For example:

student table

id name age class_id
1 yang twenty two 1
2 su 20 1
3 fan 20 2
4 li 30 2
5 luo twenty two

class table c

id name total
1 Freshman 30
2 Sophomore Year 15
3 Junior Year 40

In the table above, record number 5 in table s cannot be found in table c.

1. Left join: the left side of the left join is the main table, and if there is no corresponding secondary table, NULL is displayed.

SELECT s.`name`,s.`class_id` FROM student s LEFT JOIN class c ON s.`class_id`=c.`class_id`

result


name class_id
yang 1
su 1
fan 2
li 2
luo (NULL)

2. Right join: the right side of the right join is the primary table, and if there is no corresponding secondary table, NULL is displayed.

SELECT s.`name`,s.`class_id` FROM student s RIGHT JOIN class c ON s.`class_id`=c.`class_id`

result


name class_id
yang 1
su 1
fan 2
li 2
(NULL) (NULL)

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL 8.0.18 Hash Join does not support left/right join left and right join issues
  • mysql join query (left join, right join, inner join)
  • SQL four types of joins - left outer join, right outer join, inner join, full join detailed explanation
  • MySQL table LEFT JOIN left join and RIGHT JOIN right join example tutorial
  • Examples of using MySQL left and right inner joins
  • In-depth understanding of the four types of SQL joins - left outer join, right outer join, inner join, full join
  • SQL left join and right join usage tips (left join and right join)
  • mysql left join, right join and inner join
  • How to write a SQL statement for a three-table left join query

<<:  Quickly solve the problem of slow Tomcat startup, super simple

>>:  Detailed explanation of axios encapsulation and API interface management in React project

Recommend

JS implements sliding up and down on the mobile terminal one screen at a time

This article shares with you the specific code of...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...

...

Vue3+script setup+ts+Vite+Volar project

Table of contents Create a vue + ts project using...

Detailed explanation of small state management based on React Hooks

Table of contents Implementing state sharing base...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Deployment and Chinese translation of the docker visualization tool Portainer

#docker search #docker pull portainer 1. Download...

Example of how nginx implements dynamic and static separation

Table of contents Deploy nginx on server1 Deploy ...

How to implement responsive layout with CSS

Implementing responsive layout with CSS Responsiv...

Detailed explanation of the basic use of react-navigation6.x routing library

Table of contents react-native project initializa...

Analysis of the principle and usage of MySQL continuous aggregation

This article uses examples to illustrate the prin...

Tips for designing photo preview navigation on web pages

<br />Navigation does not just refer to the ...

The shortest JS to determine whether it is IE6 (IE writing method)

Commonly used JavaScript code to detect which ver...