Inner join, outer link (left join, right join, full join), cross join summary

Reprinted from  inner join, outer link (left join, right join, full join), cross join summary

1. What is a join query?

    Concept: Query data from two or more tables based on the relationship between the columns of these tables.

    Purpose: To implement multiple table query operations.


2. Classification:


First, divide it into three types: inner join, outer join, and cross join   (INNER JOIN):
  

  
    There are three types: equi-join, natural join, and unequal join   (OUTER JOIN):
      
 

 
    There are three types:  
    left outer join (LEFT OUTER JOIN or LEFT JOIN),  
    right outer join (RIGHT OUTER JOIN or RIGHT JOIN),  
    full outer join (FULL OUTER JOIN or FULL JOIN),   cross join (CROSS JOIN):  
  


    Without a WHERE clause, it returns the Cartesian product of all rows in the joined table 

3. Specific use introduction

    The join condition can be specified in the FROM or WHERE clause, it is recommended to specify the join condition in the FROM clause. The WHERE and HAVING clauses can also contain search conditions to further filter the rows selected by the join condition.    
Joins can be divided into the following categories:
   

Specific case table: 

book表a:                                


                          

stu table b:



1. Inner join

Definition: Only the rows from the two tables that satisfy the join condition are combined as a result set.
In an inner join, only rows that match in both tables can appear in the result set with the
keyword: INNER JOIN
format:

SELECT 列名表 FROM 表名1 [INNER] JOIN 表名2 ON或WHERE 条件表达式   

内连接分类:

等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接表中的所有列,包括其中的重复列

不等值连接:在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值。这些运算符包括>、>=、<=、<、!>、!<和<>

自然连接:在连接条件中使用等于(=)运算符比较被连接列的列值,但它使用选择列表指出查询结果集合中所包括的列,并删除连接表中的重复列。

SQL语句:
1、select * from book as a,stu as b where a.sutid = b.stuid  
2、select * from book as a inner join stu as b on a.sutid = b.stuid  
//内连接可以使用上面两种方式,其中第二种方式的inner可以省略。  
结果:展示结果相同的id列




2、外联接

外联接可以是左向外联接、右向外联接或完整外部联接。   

大概意思就是:在内连接的基础上,还包含表中所有不符合条件的数据行,并将相对应的表列填写NULL  (左--对应右NULL)
在 FROM子句中指定外联接时,可以由下列几组关键字中的一组指定:     

1)LEFT  JOIN或LEFT OUTER JOIN     
左向外联接的结果集包括  LEFT OUTER子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。   

select * from book as a left join stu as b on a.sutid = b.stuid  

2)RIGHT  JOIN 或 RIGHT  OUTER  JOIN     

右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。

select * from book as a right join stu as b on a.sutid = b.stuid  

3)FULL  JOIN 或 FULL OUTER JOIN

完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。   

select * from book as a full outer join stu as b on a.sutid = b.stuid  

3、交叉联接   

交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积    

select * from book as a cross join stu as b order by a.id  

附录一个高大上的图解:

解释一下: 1.上图(来源网上大神)左边两个为左连接的例子图解

       2.上图右边两个为右连接的例子图解

       3.上图中间一个为内连接的例子图解

       4.上图下面两个为全连接的例子图解            

参考:

http://www.cnblogs.com/afirefly/archive/2010/10/08/1845906.html

http://blog.sina.com.cn/s/blog_9bf70eb101012va6.html

http://dataunion.org/11954.html

http://www.jb51.net/article/39432.htm



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325536371&siteId=291194637