Summary of four situations of joint query between two tables in Mysql

Summary of four situations of joint query between two tables in Mysql

Generally speaking, in order to get more complete results, we need to get results from two or more tables. I usually use select xxx,xxx from table1, table2 where table1.xxx=table2.xxx. We usually perform such operations. In fact, there is another operation in MySQL, that is, join operation. For example, there are two tables below:

Take a look at the "Persons" table:

Next, look at the "Orders" table:

The above two tables, then we connect them to query

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P

Result Set:

If you use the keyword JOIN to get data from two tables

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName

Result Set:

Then the inner join is no different from the normal query above

Different SQL JOINs

  • JOIN: Returns rows if there is at least one match in the table
  • LEFT JOIN: Returns all rows from the left table even if there are no matches in the right table
  • RIGHT JOIN: Returns all rows from the right table even if there is no match in the left table
  • FULL JOIN: Returns rows if there is a match in either table.

LEFT JOIN Keyword Syntax

"Persons" table:

"Orders" table:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

Result Set:

SQL RIGHT JOIN Keyword

"Persons" table:

"Orders" table:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

Result Set:

SQL FULL JOIN keyword

"Persons" table:

"Orders" table:

FULL JOIN Example

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

Result Set:

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • MySQL database aggregate query and union query operations
  • MySQL beginners can say goodbye to the troubles of grouping and aggregation queries
  • Introduction to the use of MySQL joint query UNION and UNION ALL
  • Detailed analysis and optimization of Mysql multi-table joint query efficiency
  • Popular explanation of several MySQL joint queries
  • Analysis of MySQL multi-table joint query operation examples
  • MySQL aggregate query and union query operation examples

<<:  Implementation of React configuration sub-routing

>>:  Detailed explanation of basic management of KVM virtualization in CentOS7

Recommend

Implementation example of scan code payment in vue project (with demo)

Table of contents Demand background Thought Analy...

A complete list of common Linux system commands for beginners

Learning Linux commands is the biggest obstacle f...

JavaScript removes unnecessary properties of an object

Table of contents Example Method 1: delete Method...

jQuery implements ad display and hide animation

We often see ads appear after a few seconds and t...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

Detailed steps for installing Harbor, a private Docker repository

The installation of Harbor is pretty simple, but ...

MySQL 8.0.15 installation and configuration graphic tutorial under Win10

This article records the installation and configu...

JavaScript method to detect the type of file

Table of contents 1. How to view the binary data ...

How to Understand and Identify File Types in Linux

Preface As we all know, everything in Linux is a ...

How to enable or disable SSH for a specific user or user group in Linux

Due to your company standards, you may only allow...

Docker sets up port mapping, but cannot access the solution

#docker ps check, all ports are mapped CONTAINER ...

How to bind domain name to nginx service

Configure multiple servers in nginx.conf: When pr...