mysql join、inner join、left join 、right join 详解

join & inner join

01

理解为“有效连接”,两张表中都有的数据才会显示。

MySQL [test_join]> show tables;
+---------------------+
| Tables_in_test_join |
+---------------------+
| table_A             |
| table_B             |
+---------------------+
2 rows in set (0.00 sec)

MySQL [test_join]> select * from table_A;
+----+--------+
| id | a_name |
+----+--------+
|  1 | aaa    |
|  2 | bbb    |
|  3 | ccc    |
+----+--------+
3 rows in set (0.00 sec)

MySQL [test_join]> select * from table_B;
+----+-------------------------+--------+
| id | aid (table_A表中的主键id) | b_name |
+----+-------------------------+--------+
|  1 |          1              | 111    |
|  2 |          1              | 222    |
|  3 |          2              | 333    |
|  4 |          1              | 444    |
+----+-------------------------+--------+
4 rows in set (0.00 sec)

MySQL [test_join]> select * from table_A A join table_B B on A.id = B.aid;
+----+--------+----+------+--------+
| id | a_name | id | aid  | b_name |
+----+--------+----+------+--------+
|  1 | aaa    |  1 |    1 | 111    |
|  1 | aaa    |  2 |    1 | 222    |
|  2 | bbb    |  3 |    2 | 333    |
|  1 | aaa    |  4 |    1 | 444    |
+----+--------+----+------+--------+
4 rows in set (0.01 sec)

MySQL [test_join]> select * from table_A A inner join table_B B on A.id = B.aid;
+----+--------+----+------+--------+
| id | a_name | id | aid  | b_name |
+----+--------+----+------+--------+
|  1 | aaa    |  1 |    1 | 111    |
|  1 | aaa    |  2 |    1 | 222    |
|  2 | bbb    |  3 |    2 | 333    |
|  1 | aaa    |  4 |    1 | 444    |
+----+--------+----+------+--------+
4 rows in set (0.01 sec)

02

left join

03

以左表为主, 出左表全部数据, 右表不存在数据 null 补全。

MySQL [test_join]> select * from table_A A left join table_B B on A.id = B.aid;
+----+--------+------+------+--------+
| id | a_name | id   | aid  | b_name |
+----+--------+------+------+--------+
|  1 | aaa    |    1 |    1 | 111    |
|  1 | aaa    |    2 |    1 | 222    |
|  2 | bbb    |    3 |    2 | 333    |
|  1 | aaa    |    4 |    1 | 444    |
|  3 | ccc    | NULL | NULL | NULL   |
+----+--------+------+------+--------+
5 rows in set (0.00 sec)

right join

04

以右表为主, 出右表全部数据, 左表不存在数据 null 补全。

MySQL [test_join]> select * from table_B B right join table_A A on B.aid = A.id;
+------+------+--------+----+--------+
| id   | aid  | b_name | id | a_name |
+------+------+--------+----+--------+
|    1 |    1 | 111    |  1 | aaa    |
|    2 |    1 | 222    |  1 | aaa    |
|    3 |    2 | 333    |  2 | bbb    |
|    4 |    1 | 444    |  1 | aaa    |
| NULL | NULL | NULL   |  3 | ccc    |
+------+------+--------+----+--------+
5 rows in set (0.01 sec)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IARNO_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值