MySQL中表的几种连接方式

时间:2021-05-02

MySQL表中的连接方式其实非常简单,这里就简单的罗列出他们的特点。

表的连接(JOIN)可以分为内连接(JOIN/INNER JOIN)和外连接(LEFT JOIN/RIGHT JOIN)。

首先我们看一下我们本次演示的两个表:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 mysql> SELECT * FROM student; +------+----------+------+------+ | s_id | s_name | age | c_id | +------+----------+------+------+ | 1 | xiaoming | 13 | 1 | | 2 | xiaohong | 41 | 4 | | 3 | xiaoxia | 22 | 3 | | 4 | xiaogang | 32 | 1 | | 5 | xiaoli | 41 | 2 | | 6 | wangwu | 13 | 2 | | 7 | lisi | 22 | 3 | | 8 | zhangsan | 11 | 9 | +------+----------+------+------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM class; +------+---------+-------+ | c_id | c_name | count | +------+---------+-------+ | 1 | MATH | 65 | | 2 | CHINESE | 70 | | 3 | ENGLISH | 50 | | 4 | HISTORY | 30 | | 5 | BIOLOGY | 40 | +------+---------+-------+ 5 rows in set (0.00 sec)

首先,表要能连接的前提就是两个表中有相同的可以比较的列。

1.内连接

? 1 2 3 4 5 6 7 8 9 10 11 12 13 mysql> SELECT * FROM student INNER JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | +------+----------+------+------+------+---------+-------+ 7 rows in set (0.00 sec)

简单的讲,内连接就是把两个表中符合条件的行的所有数据一起展示出来,即如果不符合条件,即在表A中找得到但是在B中没有(或者相反)的数据不予以显示。

2.外连接

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 mysql> SELECT * FROM student LEFT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 8 | zhangsan | 11 | 9 | NULL | NULL | NULL | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM student RIGHT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | NULL | NULL | NULL | NULL | 5 | BIOLOGY | 40 | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec)

上面分别展示了外连接的两种情况:左连接和右连接。这两种几乎是一样的,唯一的区别就是左连接的主表是左边的表,右连接的主表是右边的表。而外连接与内连接不同的地方就是它会将主表的所有行都予以显示,而在主表中有,其他表中没有的数据用NULL代替。

总结

到此这篇关于MySQL中表的几种连接方式的文章就介绍到这了,更多相关MySQL表的连接方式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://segmentfault.com/a/1190000038203698

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章