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

MySQL 8.0.12 installation and configuration graphic tutorial

Recorded the download and installation tutorial o...

Solution to the problem that order by is not effective in MySQL subquery

By chance, I discovered that a SQL statement prod...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

Vue3.0 routing automatic import method example

1. Prerequisites We use the require.context metho...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

MySQL deduplication methods

MySQL deduplication methods 【Beginner】There are v...

Implementation of setting fixed IP when starting docker container

Network type after docker installation [root@insu...

Detailed explanation of common MySQL operation commands in Linux terminal

Serve: # chkconfig --list List all system service...

Angular Cookie read and write operation code

Angular Cookie read and write operations, the cod...

Linux automatic login example explanation

There are many scripts on the Internet that use e...

Analysis of MySQL query sorting and query aggregation function usage

This article uses examples to illustrate the use ...

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...