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

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

An article to deal with Mysql date and time functions

Table of contents Preface 1. Get the current time...

Detailed explanation of Linux file permissions and group modification commands

In Linux, everything is a file (directories are a...

Use docker to build kong cluster operation

It is very simple to build a kong cluster under t...

Vue implements paging function

This article example shares the specific code of ...

How to start Vue project with M1 pro chip

Table of contents introduction Install Homebrew I...

Use of LRU algorithm in Vue built-in component keep-alive

Table of contents The use of Vue's keep-alive...

How to set up remote access to a server by specifying an IP address in Windows

We have many servers that are often interfered wi...

Create an SSL certificate that can be used in nginx and IIS

Table of contents Creating an SSL Certificate 1. ...

How to configure https for nginx in docker

Websites without https support will gradually be ...

Docker installation steps for Redmine

Download the image (optional step, if omitted, it...

Detailed explanation of Linux system directories sys, tmp, usr, var!

The growth path from a Linux novice to a Linux ma...

Docker installs redis 5.0.7 and mounts external configuration and data issues

Redis is an open source NoSQL database written in...

Jenkins packaging microservices to build Docker images and run them

Table of contents Environment Preparation start 1...