MySQL: Left Join, Right Join and Inner Join, Outer Join

Example to explain differences of left join and right join :

#################
select * from goods;
+----------+--------+------------+
| goods_id | cat_id | goods_name |
+----------+--------+------------+
|        1 |      1 | CDMA Phone |
|        2 |      1 | GSM Phone  |
|        3 |      1 | 3G Phone   |
|        4 |      3 | TP Phone   |
+----------+--------+------------+
select * from cat;
+--------+---------------+
| cat_id | cat_name      |
+--------+---------------+
|      1 | Mobile Phone  |
|      2 | Settled Phone |
+--------+---------------+
#################

#Left join: Use left table as criterion.
#Pay attention to the last row.
select goods.goods_id, goods.cat_id, goods.goods_name, cat.cat_name from goods left join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------------+
| goods_id | cat_id | goods_name | cat_name     |
+----------+--------+------------+--------------+
|        1 |      1 | CDMA Phone | Mobile Phone |
|        2 |      1 | GSM Phone  | Mobile Phone |
|        3 |      1 | 3G Phone   | Mobile Phone |
|        4 |      3 | TP Phone   | NULL         |
+----------+--------+------------+--------------+
select goods.*, cat.* from cat left join goods on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+---------------+
| goods_id | cat_id | goods_name | cat_id | cat_name      |
+----------+--------+------------+--------+---------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone  |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone  |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone  |
| NULL     | NULL   | NULL       |      2 | Settled Phone |
+----------+--------+------------+--------+---------------+

#Right join: Use right table as criterion
select goods.*, cat.* from cat right join goods on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+--------------+
| goods_id | cat_id | goods_name | cat_id | cat_name     |
+----------+--------+------------+--------+--------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone |
|        4 |      3 | TP Phone   | NULL   | NULL         |
+----------+--------+------------+--------+--------------+
select goods.*, cat.* from goods right join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+---------------+
| goods_id | cat_id | goods_name | cat_id | cat_name      |
+----------+--------+------------+--------+---------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone  |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone  |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone  |
| NULL     | NULL   | NULL       |      2 | Settled Phone |
+----------+--------+------------+--------+---------------+

#Comment:
#table A left join table B == table B right join table A
#Both use table A as criterion
#Use the table that actually stands at the LEFT side as criterion

 

Example to explain Inner Join and Outter Join:

#Use the previous table

#Left join
select goods.goods_id, goods.cat_id, goods.goods_name, cat.cat_name from goods left join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------------+
| goods_id | cat_id | goods_name | cat_name     |
+----------+--------+------------+--------------+
|        1 |      1 | CDMA Phone | Mobile Phone |
|        2 |      1 | GSM Phone  | Mobile Phone |
|        3 |      1 | 3G Phone   | Mobile Phone |
|        4 |      3 | TP Phone   | NULL         |
+----------+--------+------------+--------------+

#Right join
select goods.*, cat.* from cat left join goods on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+---------------+
| goods_id | cat_id | goods_name | cat_id | cat_name      |
+----------+--------+------------+--------+---------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone  |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone  |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone  |
| NULL     | NULL   | NULL       |      2 | Settled Phone |
+----------+--------+------------+--------+---------------+

#Inner join
select goods.*, cat.* from goods inner join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+--------------+
| goods_id | cat_id | goods_name | cat_id | cat_name     |
+----------+--------+------------+--------+--------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone |
+----------+--------+------------+--------+--------------+

#Outter join
select goods.*, cat.* from goods left join cat on goods.cat_id = cat.cat_id union select goods.*, cat.* from goods right join cat on goods.cat_id = cat.cat_id;
+----------+--------+------------+--------+---------------+
| goods_id | cat_id | goods_name | cat_id | cat_name      |
+----------+--------+------------+--------+---------------+
|        1 |      1 | CDMA Phone |      1 | Mobile Phone  |
|        2 |      1 | GSM Phone  |      1 | Mobile Phone  |
|        3 |      1 | 3G Phone   |      1 | Mobile Phone  |
|        4 |      3 | TP Phone   | NULL   | NULL          |
| NULL     | NULL   | NULL       |      2 | Settled Phone |
+----------+--------+------------+--------+---------------+

   Comment:

        1) Inner join will not use left table or right table as criterion.

        2) Just think as table A cartesian product table B. And then apply the filter on the result set.

        3) Or regard  Inner Join as the Intersection of Left Join and Right Join.

        4) So how to get the Union of Left Join and Right Join? ----> Outter Join is not supported by MySQL!----> But we can use UNION to combine the result set of left join and right join to realize this!

 

Comments:

    1) How to join more than to tables?---->Will be explained in detail in next charpter.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值