【JOIN】关键字在MySql中的详细使用

目录

[INNER JOIN(内连接)](#INNER JOIN(内连接))

[LEFT JOIN(左连接)](#LEFT JOIN(左连接))

[RIGHT JOIN(右连接)](#RIGHT JOIN(右连接))

[FULL JOIN(全连接)](#FULL JOIN(全连接))

示例图形化解释JOIN的不同类型

[INNER JOIN:](#INNER JOIN:)

[LEFT JOIN:](#LEFT JOIN:)

[RIGHT JOIN:](#RIGHT JOIN:)

[FULL JOIN:](#FULL JOIN:)


INNER JOIN(内连接)

返回两个表中匹配的行。

表达方式1:

复制代码
SELECT * 
FROM table1 
INNER JOIN table2 
ON table1.column = table2.column;

表达方式2:

复制代码
SELECT * 
FROM table1, table2 
WHERE table1.column = table2.column;
LEFT JOIN(左连接)

返回左表中的所有行以及与右表匹配的行。

表达方式1:

复制代码
SELECT * 
FROM table1 
LEFT JOIN table2 
ON table1.column = table2.column;

表达方式2:

复制代码
SELECT * 
FROM table1 
LEFT OUTER JOIN table2 
ON table1.column = table2.column;
RIGHT JOIN(右连接)

返回右表中的所有行以及与左表匹配的行。

表达方式1:

复制代码
SELECT * 
FROM table1 
RIGHT JOIN table2 
ON table1.column = table2.column;

表达方式2:

复制代码
SELECT * 
FROM table1 
RIGHT OUTER JOIN table2 
ON table1.column = table2.column;
FULL JOIN(全连接)

返回左右表中的所有行。

复制代码
SELECT * 
FROM table1 
FULL JOIN table2 
ON table1.column = table2.column;
示例图形化解释JOIN的不同类型
INNER JOIN
复制代码
+----+-------+-----+-------+
| ID | Name  | Age | Grade |
+----+-------+-----+-------+
| 1  | John  | 24  | A     |
| 2  | Alice | 22  | B     |
| 3  | Bob   | 25  | A     |
+----+-------+-----+-------+

+--------+--------+
| ID     | City   |
+--------+--------+
| 1      | London |
| 2      | Paris  |
| 4      | Berlin |
+--------+--------+

After INNER JOIN:

+----+-------+-----+-------+--------+--------+
| ID | Name  | Age | Grade | ID     | City   |
+----+-------+-----+-------+--------+--------+
| 1  | John  | 24  | A     | 1      | London |
| 2  | Alice | 22  | B     | 2      | Paris  |
+----+-------+-----+-------+--------+--------+
LEFT JOIN
复制代码
+----+-------+-----+-------+
| ID | Name  | Age | Grade |
+----+-------+-----+-------+
| 1  | John  | 24  | A     |
| 2  | Alice | 22  | B     |
| 3  | Bob   | 25  | A     |
+----+-------+-----+-------+

+--------+--------+
| ID     | City   |
+--------+--------+
| 1      | London |
| 2      | Paris  |
| 4      | Berlin |
+--------+--------+

After LEFT JOIN:

+----+-------+-----+-------+--------+--------+
| ID | Name  | Age | Grade | ID     | City   |
+----+-------+-----+-------+--------+--------+
| 1  | John  | 24  | A     | 1      | London |
| 2  | Alice | 22  | B     | 2      | Paris  |
| 3  | Bob   | 25  | A     | NULL   | NULL   |
+----+-------+-----+-------+--------+--------+
RIGHT JOIN
复制代码
+----+-------+-----+-------+
| ID | Name  | Age | Grade |
+----+-------+-----+-------+
| 1  | John  | 24  | A     |
| 2  | Alice | 22  | B     |
| 3  | Bob   | 25  | A     |
+----+-------+-----+-------+

+--------+--------+
| ID     | City   |
+--------+--------+
| 1      | London |
| 2      | Paris  |
| 4      | Berlin |
+--------+--------+

After RIGHT JOIN:

+----+-------+-----+-------+--------+--------+
| ID | Name  | Age | Grade | ID     | City   |
+----+-------+-----+-------+--------+--------+
| 1  | John  | 24  | A     | 1      | London |
| 2  | Alice | 22  | B     | 2      | Paris  |
| NULL | NULL  | NULL   | NULL    | 4      | Berlin |
+----+-------+-----+-------+--------+--------+
FULL JOIN
复制代码
+----+-------+-----+-------+
| ID | Name  | Age | Grade |
+----+-------+-----+-------+
| 1  | John  | 24  | A     |
| 2  | Alice | 22  | B     |
| 3  | Bob   | 25  | A     |
+----+-------+-----+-------+

+--------+--------+
| ID     | City   |
+--------+--------+
| 1      | London |
| 2      | Paris  |
| 4      | Berlin |
+--------+--------+

After FULL JOIN:

+----+-------+-----+-------+--------+--------+
| ID | Name  | Age | Grade | ID     | City   |
+----+-------+-----+-------+--------+--------+
| 1  | John  | 24  | A     | 1      | London |
| 2  | Alice | 22  | B     | 2      | Paris  |
| 3  | Bob   | 25  | A     | NULL   | NULL   |
| NULL | NULL  | NULL   | NULL    | 4      | Berlin |
+----+-------+-----+-------+--------+--------+
相关推荐
Code Warrior1 小时前
【MySQL数据库】使用C语言连接
数据库·mysql
SoleMotive.1 小时前
如果用户反映页面跳转得非常慢,该如何排查
jvm·数据库·redis·算法·缓存
U***l8321 小时前
【postgresql】分区表管理
java·数据库·postgresql
p***23361 小时前
python的sql解析库-sqlparse
数据库·python·sql
s***11701 小时前
Mysql convert函数、convert用法、字符串转数字、字符串转日期、类型转换函数
android·数据库·mysql
S***q3771 小时前
【Mysql】:如何恢复误删的数据?
数据库·mysql
s***P9821 小时前
Spring数据库原理 之 DataSource
java·数据库·spring
老朱佩琪!1 小时前
Unity如何连接本地MySQL数据库
数据库·mysql
JIngJaneIL1 小时前
农产品电商|基于SprinBoot+vue的农产品电商系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·农产品电商系统