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.

你可能感兴趣的:(MySQL,Left,Join,Right,Join,Inner,Join,Outer,Join)