join

join   是inner   join的简写形式。
1     INNER   JOIN
INNER   JOIN是组合两个表最常用的方法。INNER   JOIN是基于一个判别式进行的,这个判别式称为连接条件。连接条件和WHERE子句一起定义。连接条件由来自两个表中的列组成,并使用一个比较条件来对列的值进行比较。通过比较的值包含在结果数据集中,以下是Inner   JOIN的语法:

语法1:(ANSI   92)
Select   <select_list>
FROM   <table1>   INNER   JOIN   <table2>
ON   <table1> . <column   name>   =   <table2> . <column   name>

语法2:
Select   <select_list>
FROM   <table1> , <table2>   WHERE   <table1> . <column   name>   =   <table2> . <column   name>
在FROM   子句中可为表定义别名,并在任何地方都可用别名代替真名。
注意:如果作为连接条件的列中有空值,则空值不能和任何值匹配,因此结果中不包含有空值的行。

 


2、outer   join   又分left   outer   join 、 right   outer   join 、 full outer join

 

Left   Outer   JOIN
在Inner   JOIN中,只有在两个表中匹配的行才能在结果数据集中。但在Left   Outer   JOIN中,所有左边表中的行都出现在结果数据集中,如果左边表中的某一行在右边表中没有匹配的行,则以空值取代右边表中的值和它连接。
语法如下:(ANSI   92)
Select   <select_list>
FROM   <table1>   LEFT   OUTER   JOIN   <table2>
ON   <table1> . <column   name>   =   <table2> . <column   name>

 

Right   Outer   JOIN
Right   Out   JOIN和Left   Outer   JOIN相似,不同的是把右边的表作为外部表(所有右边表中的行包含在结果数据集中)。
语法如下:
Select   <select_list>
FROM   <table1>   RIGHT   OUTER   JOIN   <table2>
ON   <table1> . <column   name>   =   <table2> . <column   name>

 

Full   Outer   JOIN
在Full   Outer   JOIN中,所有两个表中的行都包含在结果数据集中。
语法如下:
Select   <select_list>
FROM   <table1>   FULL   OUTER   JOIN   <table2>
ON   <table1> . <column   name>   =   <table2> . <column   name>

你可能感兴趣的:(JOIN)