mysql join query (left join, right join, inner join)

mysql join query (left join, right join, inner join)

1. Common connections for mysql

  • INNER JOIN (inner join, or equijoin): Get records with matching fields in two tables.
  • LEFT JOIN: Get all records in the left table, even if there are no corresponding matching records in the right table.
  • RIGHT JOIN: The opposite of LEFT JOIN, used to obtain all records in the right table, even if there are no corresponding matching records in the left table.
mysql> select * from name_address;
+----------+------+----+
| address | name | id |
+----------+------+----+
| Northwest Road | Zhang San | 1 |
| Northwest Second Road | Li Si | 2 |
| Northwest Third Road | Wang Wu | 3 |
+----------+------+----+
3 rows in set

mysql> select * from name_age;
+-----+--------+----+
| age | name | id |
+-----+--------+----+
| 18 | Zhang San | 1 |
| 20 | Wang Wu | 2 |
| 21 | Passerby | 3 |
+-----+--------+----+
3 rows in set

1. INNER JOIN

INNER JOIN is consistent with general table join query, that is, the query method is separated by commas.

mysql> SELECT a.`name`,a.age,b.address FROM name_age a INNER JOIN name_address b WHERE (on) a.`name`=b.`name`;
+------+-----+----------+
| name | age | address |
+------+-----+----------+
| Zhang San | 18 | Northwest Road |
| Wang Wu| 20 | Northwest Third Road|
+------+-----+----------+
2 rows in set

2. LEFT JOIN

Please refer to the data table on the left.

mysql> SELECT a.`name`,a.age,b.address FROM name_age a left JOIN name_address b on
 a.`name`=b.`name`;
+--------+-----+----------+
| name | age | address |
+--------+-----+----------+
| Zhang San | 18 | Northwest Road |
| Wang Wu| 20 | Northwest Third Road|
| Passerby| 21 | NULL |
+--------+-----+----------+
3 rows in set

3. RIGHT JOIN

The opposite of LEFT JOIN, that is, the data on the right is used as the basis

mysql> SELECT b.`name`,a.age,b.address FROM name_age a right JOIN name_address b on a.`name`=b.`name`;
+------+------+----------+
| name | age | address |
+------+------+----------+
| Zhang San | 18 | Northwest Road |
| Wang Wu| 20 | Northwest Third Road|
| Li Si | NULL | Northwest Second Road |
+------+------+----------+
3 rows in set

The above is the information compilation of MySQL connection query. If you have any questions, you can leave a message to discuss and make progress together. Thank you for reading and hope to help everyone. Thank you for your support of this site!

You may also be interested in:
  • Briefly talk about mysql left join inner join
  • MYSQL left join right join and inner join detailed explanation and difference
  • mysql left join, right join and inner join
  • Detailed explanation of mysql inner join, left join and right join query

<<:  Tips for writing concise React components

>>:  View the number of files in each subfolder of a specified folder in Linux

Recommend

MySQL infobright installation steps

Table of contents 1. Use the "rpm -ivh insta...

Mysql optimization techniques for querying dates based on time

For example, to query yesterday's newly regis...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...

JavaScript file loading and blocking issues: performance optimization case study

Let me start with a question: When writing an HTM...

Use VSCode's Remote-SSH to connect to Linux for remote development

Install Remote-SSH and configure it First open yo...

How are spaces represented in HTML (what do they mean)?

In web development, you often encounter characters...

How to express relative paths in Linux

For example, if your current path is /var/log and...

A complete record of a Mysql deadlock troubleshooting process

Preface The database deadlocks I encountered befo...

Detailed explanation of JavaScript clipboard usage

(1) Introduction: clipboard.js is a lightweight J...

A complete tutorial on using axios encapsulation in vue

Preface Nowadays, in projects, the Axios library ...