准备测试表tb1和tb2并插入数据https://www.cndba.cn/hbhe0316/article/22644
https://www.cndba.cn/hbhe0316/article/22644
https://www.cndba.cn/hbhe0316/article/22644
https://www.cndba.cn/hbhe0316/article/22644
https://www.cndba.cn/hbhe0316/article/22644
mysql> select * from tb1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)
mysql> select * from tb2;
+------+
| id |
+------+
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+------+
5 rows in set (0.00 sec)
1.左外部链接:匹配A、B两个数据集,仅返回A中存在的数据。
https://www.cndba.cn/hbhe0316/article/22644
https://www.cndba.cn/hbhe0316/article/22644
mysql> select * from tb1 left join tb2 on tb1.id=tb2.id;
+------+------+
| id | id |
+------+------+
| 4 | 4 |
| 5 | 5 |
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
+------+------+
5 rows in set (0.00 sec)
2.右外部连接:匹配A、B两个数据集,仅返回B中存在的数据。https://www.cndba.cn/hbhe0316/article/22644https://www.cndba.cn/hbhe0316/article/22644https://www.cndba.cn/hbhe0316/article/22644
mysql> select * from tb1 right join tb2 on tb1.id=tb2.id;
+------+------+
| id | id |
+------+------+
| 4 | 4 |
| 5 | 5 |
| NULL | 6 |
| NULL | 7 |
| NULL | 8 |
+------+------+
5 rows in set (0.00 sec)
3.内部连接:匹配A、B两个数据集,仅返回A、B均存在的数据。
mysql> select * from tb1 inner join tb2 on tb1.id=tb2.id;
+------+------+
| id | id |
+------+------+
| 4 | 4 |
| 5 | 5 |
+------+------+
2 rows in set (0.00 sec)
版权声明:本文为博主原创文章,未经博主允许不得转载。
MYSQL