已经建立两个表,test1,test2
1.
执行 select * from test1
得到:
————————
id name
————————
1 tom
______________
2 jerry
2.
执行 select * from test2
得到:
————————
id name
————————
1 huahua
______________
3 linda
3.
左连接:
select * from test1 left join test2 on test1.id = test2.id;
得到:
+----+-------+------+--------+
| id | name | id | name |
+----+-------+------+--------+
| 1 | tom | 1 | huahua |
| 2 | jerry | NULL | NULL |
+----+-------+------+--------+
4.
右连接:
select * from test1 right join test2 on test1.id = test2.id;
得到:
+------+------+----+--------+
| id | name | id | name |
+------+------+----+--------+
| 1 | tom | 1 | huahua |
| NULL | NULL | 3 | linda |
+------+------+----+--------+
从两张结果图的结果就可以看出,左连接就是以左边的表为基准表,左边的表全部显示,右边的表只显示匹配内容。
右连接就是以右边的表为基准,右边的表全部显示,左边的表只显示匹配内容
再看内连接:
select * from test1 inner join test2 on test1.id = test2.id;
得到:
+----+------+----+--------+
| id | name | id | name |
+----+------+----+--------+
| 1 | tom | 1 | huahua |
+----+------+----+--------+
可以看出内连接只显示匹配的内容
MySQL 左连接,右连接,内连接 理解
最新推荐文章于 2023-11-25 22:40:07 发布