`
DavyJones2010
  • 浏览: 154890 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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.

分享到:
评论

相关推荐

    关于sql的left join,right join,inner join,outerjoin

    本篇文章将深入探讨四种基本的JOIN类型:LEFT JOIN(左连接)、RIGHT JOIN(右连接)、INNER JOIN(内连接)以及OUTER JOIN(外连接)。我们将详细解释这些概念,它们的工作原理以及在实际数据库操作中的应用。 1. ...

    Mysql之innerjoin,leftjoin,rightjoin详解.pdf

    Mysql 之 inner join、left join、right join 详解 Mysql 中的连接查询是指从多个表中检索数据,并将它们组合成一个结果集。inner join、left join 和 right join 是 Mysql 中三种最常用的连接查询方式。 inner ...

    sql join( inner join, outer join) 分析

    接下来,我们讨论OUTER JOIN,它分为LEFT JOIN、RIGHT JOIN和FULL JOIN。OUTER JOIN的目的是返回所有匹配的行以及至少一方表中的非匹配行。 - LEFT JOIN(或LEFT OUTER JOIN)返回左表中的所有行,即使右表中没有...

    SQL联合查询inner join、outer join和cross join的区别详解

    在SQL查询中,联合查询是将两个或多个表格的数据结合在一起的一种方法,主要涉及三种类型的联合:INNER JOIN、OUTER JOIN(包括LEFT JOIN、RIGHT JOIN和FULL JOIN)以及CROSS JOIN。理解这些概念对于数据库开发人员...

    mysql join所有方法总结

    在MySQL中,主要的Join方法包括INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN、SELF JOIN以及CROSS JOIN等。 INNER JOIN(内连接):是所有Join中最常见的类型。使用INNER JOIN后,只有两个表中能够相互匹配的记录...

    Joins_MySQL:学习和实践JOIN

    2. **LEFT JOIN (或 LEFT OUTER JOIN)**: LEFT JOIN返回所有左表(在ON语句之前指定的表)的记录,即使右表中没有匹配的记录。如果找不到匹配,结果将是NULL。其语法类似于INNER JOIN,但增加了LEFT关键字: ```...

    MySQL JOIN之完全用法

    3. **右外联接(RIGHT JOIN 或 RIGHT OUTER JOIN)**:右外联接与左外联接相反,返回右表的所有行,即使在左表中没有匹配的行。对于右表中那些在左表中没有匹配的行,结果集中相应左表的字段将填充为NULL。 4. **全外...

    MySQL中的JOIN详解及sql实战

    MySQL不支持标准的FULL OUTER JOIN语法,但可以通过联合使用LEFT JOIN和RIGHT JOIN来实现类似的功能。 #### 三、具体示例分析 假设我们有两个表`user_sal`,表结构如下: ```sql CREATE TABLE `user_sal` ( `id`...

    万里长城第一步-先来学好数据库之MySQL上.docx

    * right outer join:select * from tbl_emp a right join tbl_dept b on a.deptId = b.id where a.deptId is null; * full join:SELECT * FROM tbl_emp A LEFT JOIN tbl_dept B ON A.deptId = B.id UNION SELECT *...

    Mysql中的join操作

    JOIN的主要类型包括内联结(INNER JOIN)、外联结(OUTER JOIN),其中外联结又分为外左联结(LEFT JOIN)和外右联结(RIGHT JOIN)。 1. **内联结(INNER JOIN)**: 内联结返回两个表中匹配的记录。当指定的条件...

    MySQL:常用基本SQL语句小结

    - `JOIN`:连接两个或更多表,例如`INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, `FULL OUTER JOIN`。 3. 子查询: 在主查询中嵌套一个查询,用子查询结果作为主查询的一部分。 四、索引 1. 创建索引: ```sql ...

    mysql 单表多表查询

    常见的多表查询方法有JOIN、INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL OUTER JOIN等。 - **JOIN**: JOIN用于连接两个或更多表,基于这些表之间的关联。例如,`SELECT * FROM table1 JOIN table2 ON table1.id = ...

    MySQL中join语句的基本使用教程及其字段对性能的影响

    3. **RIGHT JOIN (或 RIGHT OUTER JOIN)**:与LEFT JOIN相反,返回右表的所有记录,即使在左表中没有匹配的记录。 由于MySQL不直接支持FULL JOIN,要实现全连接的效果,可以通过UNION结合LEFT JOIN和RIGHT JOIN来...

    一篇文章带你了解数据库中JOIN的用法

    本文将深入探讨JOIN的四种基本类型:内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)以及虽不常见但重要的全外连接(FULL OUTER JOIN)。 首先,我们要理解JOIN的原理,它允许我们合并两...

    了解复杂MySQL JOIN

    主要有四种类型的JOIN:INNER JOIN,LEFT JOIN,RIGHT JOIN和FULL OUTER JOIN。INNER JOIN返回两个表中匹配的行,而LEFT JOIN返回左表的所有行和右表匹配的行,反之亦然。FULL OUTER JOIN则返回所有不匹配的行。 2....

    Join on 连接

    在本篇内容中,我们将深入探讨`JOIN`的不同类型,包括`INNER JOIN`、`LEFT JOIN`、`RIGHT JOIN`以及`FULL OUTER JOIN`,并理解它们在实际应用中的差异和作用。 1. **INNER JOIN**(内连接): 内连接只返回两个表...

    mysql 一条语句删除多表数据

    在删除场景中,可以使用`INNER JOIN`、`LEFT JOIN`、`RIGHT JOIN` 或 `FULL OUTER JOIN` 来确定哪些行应该被删除。 1. **INNER JOIN**:只删除那些在所有参与表中都存在的记录。如果你知道你要删除的记录在所有关联...

    在MySQL中使用JOIN语句进行连接操作的详细教程

    除了INNER JOIN,MySQL还提供了其他类型的JOIN,包括LEFT JOIN、RIGHT JOIN和FULL OUTER JOIN: - LEFT JOIN返回左表(在JOIN语句中列出的第一个表)的所有记录,即使在右表中没有匹配的记录。如果在右表中没有匹配...

Global site tag (gtag.js) - Google Analytics