当前位置: 代码网 > it编程>数据库>Mysql > MySql中JOIN关键字详细使用

MySql中JOIN关键字详细使用

2025年12月05日 Mysql 我要评论
inner join(内连接)返回两个表中匹配的行。表达方式1:select * from table1 inner join table2 on table1.column = table2.col

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 |
+----+-------+-----+-------+--------+--------+

到此这篇关于mysql中join关键字详细使用的文章就介绍到这了,更多相关mysql json使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com