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

Get / delete method to pass array parameters in Vue

When the front-end and back-end interact, sometim...

In-depth explanation of the principle of MySQL Innodb index

introduction Looking back four years ago, when I ...

Summary of commonly used tags in HTML (must read)

Content Detail Tags: <h1>~<h6>Title T...

How to fix the width of table in ie8 and chrome

When the above settings are used in IE8 and Chrome...

Bootstrap+Jquery to achieve calendar effect

This article shares the specific code of Bootstra...

Analysis of the process of simply deploying nginx in Docker container

1. Deploy nginx service in container The centos:7...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

Vue project code splitting solution

Table of contents background Purpose Before split...

Practical record of solving MySQL deep paging problem

Table of contents Preface Why does limit deep pag...

12 Laws of Web Design for Clean Code [Graphic]

Beautiful code is the foundation of a beautiful we...

Detailed explanation of the principle and usage of MySQL stored procedures

This article uses examples to explain the princip...