mysql中left join,right join,inner join的区别

sql查询中有一个非常重要的环节就是表的关联查询,一般使用left join,right join,inner join,他们之间的区别是什么呢?

下面我们通过具体的sql语句来演示,演示用的表名为test1/test2:


mysql> select * from test1;
+----+--------+------+------+
| id | name   | age  | sex  |
+----+--------+------+------+
|  0 | 小董   | 20   | 男   |
|  1 | 小王   | 15   | 男   |
|  2 | 小李   | 18   | 男   |
|  3 | 小红   | 16   | 女   |
+----+--------+------+------+
4 rows in set (0.00 sec)


mysql> select * from test2;
+----+--------+-----------------------+-------+
| id | name   | interest              | score |
+----+--------+-----------------------+-------+
|  1 | 小王   | 打篮球                | 81    |
|  2 | 小李   | 踢足球                | 84    |
|  3 | 小红   | 看动漫                | 99    |
|  4 | 小军   | 打游戏,打篮球        | 100   |
+----+--------+-----------------------+-------+
4 rows in set (0.00 sec)


mysql> select * from test1 t1 left join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+------+--------+-----------+-------+
| id | name   | age  | sex  | id   | name   | interest  | score |
+----+--------+------+------+------+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |    1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |    2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |    3 | 小红   | 看动漫    | 99    |
|  0 | 小董   | 20   | 男   | NULL | NULL   | NULL      | NULL  |
+----+--------+------+------+------+--------+-----------+-------+
4 rows in set (0.12 sec)


mysql> select * from test2 t1 left join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------------------+-------+------+--------+------+------+
| id | name   | interest              | score | id   | name   | age  | sex  |
+----+--------+-----------------------+-------+------+--------+------+------+
|  1 | 小王   | 打篮球                | 81    |    1 | 小王   | 15   | 男   |
|  2 | 小李   | 踢足球                | 84    |    2 | 小李   | 18   | 男   |
|  3 | 小红   | 看动漫                | 99    |    3 | 小红   | 16   | 女   |
|  4 | 小军   | 打游戏,打篮球        | 100   | NULL | NULL   | NULL | NULL |
+----+--------+-----------------------+-------+------+--------+------+------+
4 rows in set (0.00 sec)


mysql> select t1.id,t1.name,t1.interest,t1.score,t2.age,t2.sex from test2 t1 left join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------------------+-------+------+------+
| id | name   | interest              | score | age  | sex  |
+----+--------+-----------------------+-------+------+------+
|  1 | 小王   | 打篮球                | 81    | 15   | 男   |
|  2 | 小李   | 踢足球                | 84    | 18   | 男   |
|  3 | 小红   | 看动漫                | 99    | 16   | 女   |
|  4 | 小军   | 打游戏,打篮球        | 100   | NULL | NULL |
+----+--------+-----------------------+-------+------+------+
4 rows in set (0.02 sec)


mysql> select t1.id,t1.name,t1.interest,t1.score,t2.age,t2.sex from test2 t1 inner join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------+-------+------+------+
| id | name   | interest  | score | age  | sex  |
+----+--------+-----------+-------+------+------+
|  1 | 小王   | 打篮球    | 81    | 15   | 男   |
|  2 | 小李   | 踢足球    | 84    | 18   | 男   |
|  3 | 小红   | 看动漫    | 99    | 16   | 女   |
+----+--------+-----------+-------+------+------+
3 rows in set (0.00 sec)


mysql> select * from test1 t1 left join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+------+--------+-----------+-------+
| id | name   | age  | sex  | id   | name   | interest  | score |
+----+--------+------+------+------+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |    1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |    2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |    3 | 小红   | 看动漫    | 99    |
|  0 | 小董   | 20   | 男   | NULL | NULL   | NULL      | NULL  |
+----+--------+------+------+------+--------+-----------+-------+
4 rows in set (0.00 sec)


mysql> select * from test1 t1 right join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+------+--------+------+------+----+--------+-----------------------+-------+
| id   | name   | age  | sex  | id | name   | interest              | score |
+------+--------+------+------+----+--------+-----------------------+-------+
|    1 | 小王   | 15   | 男   |  1 | 小王   | 打篮球                | 81    |
|    2 | 小李   | 18   | 男   |  2 | 小李   | 踢足球                | 84    |
|    3 | 小红   | 16   | 女   |  3 | 小红   | 看动漫                | 99    |
| NULL | NULL   | NULL | NULL |  4 | 小军   | 打游戏,打篮球        | 100   |
+------+--------+------+------+----+--------+-----------------------+-------+
4 rows in set (0.00 sec)


mysql> select * from test1 t1 inner join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+----+--------+-----------+-------+
| id | name   | age  | sex  | id | name   | interest  | score |
+----+--------+------+------+----+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |  1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |  2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |  3 | 小红   | 看动漫    | 99    |
+----+--------+------+------+----+--------+-----------+-------+
3 rows in set (0.00 sec)


从上面的截图可以很容易看出来三者之间的区别,left join 查出来的数据条数是以查询的表为主,right join查出来的数据条数是以关联表为主,inner join查出来的数据是在查询条件在两者的交集

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值