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

CSS solves the misalignment problem of inline-block

No more nonsense, post code HTML part <div cla...

How to prevent hyperlink redirection using JavaScript (multiple ways of writing)

Through JavaScript, we can prevent hyperlinks fro...

Regarding the Chinese garbled characters in a href parameter transfer

When href is needed to pass parameters, and the p...

Use of Vue3 pages, menus, and routes

Table of contents 1. Click on the menu to jump 1....

Implementation of running springboot project with Docker

Introduction: The configuration of Docker running...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

Implement QR code scanning function through Vue

hint This plug-in can only be accessed under the ...

Method for realizing Internet interconnection by VMware virtual machine bridging

After installing VMware and creating a new virtua...

Do you know why vue data is a function?

Official website explanation: When a component is...

MySQL count detailed explanation and function example code

Detailed explanation of mysql count The count fun...

Issues installing Python3 and Pip in ubuntu in Docker

text 1) Download the Ubuntu image docker pull ubu...

How to export mysql table structure to excel

The requirements are as follows Export the table ...