oracle中left join和right join的区别

oracle中left join和right join的区别  

1,说明:

 

通俗的讲:  

oracle中left join和right join的区别 - anly_liu - anly的博客   A   left   join    B    的连接的记录数与A表的记录数同  

oracle中left join和right join的区别 - anly_liu - anly的博客   A   right   join    B    的连接的记录数与B表的记录数同    

oracle中left join和right join的区别 - anly_liu - anly的博客   A   left   join    B    等价B   right   join    A 

2,left join

 

table    A:  

oracle中left join和right join的区别 - anly_liu - anly的博客   Field_K,    Field_A  

oracle中left join和right join的区别 - anly_liu - anly的博客  1                        a  

oracle中left join和right join的区别 - anly_liu - anly的博客  3                        b  

oracle中left join和right join的区别 - anly_liu - anly的博客  4                        c  

oracle中left join和right join的区别 - anly_liu - anly的博客   

oracle中left join和right join的区别 - anly_liu - anly的博客  table    B:  

oracle中left join和right join的区别 - anly_liu - anly的博客   Field_K,    Field_B  

oracle中left join和right join的区别 - anly_liu - anly的博客  1                        x  

oracle中left join和right join的区别 - anly_liu - anly的博客  2                        y  

oracle中left join和right join的区别 - anly_liu - anly的博客  4                        z  

oracle中left join和right join的区别 - anly_liu - anly的博客   

oracle中left join和right join的区别 - anly_liu - anly的博客  select    a.Field_K,    a.Field_A,    b.Field_K,    b.Field_B  

oracle中left join和right join的区别 - anly_liu - anly的博客  from    a   left   join    b   on    a.Field_K=b.Field_K  

oracle中left join和right join的区别 - anly_liu - anly的博客   

oracle中left join和right join的区别 - anly_liu - anly的博客   Field_K          Field_A          Field_K          Field_B          

oracle中left join和right join的区别 - anly_liu - anly的博客  ----------    ----------    ----------    ----------    

oracle中left join和right join的区别 - anly_liu - anly的博客  1                      a                     1                      x                    

oracle中left join和right join的区别 - anly_liu - anly的博客  3                      b                     NULL               NULL  

oracle中left join和right join的区别 - anly_liu - anly的博客  4                      c                     4                      z     

3,right join

 

 select    a.Field_K,    a.Field_A,    b.Field_K,    b.Field_B  

oracle中left join和right join的区别 - anly_liu - anly的博客  from    a   right   join    b   on    a.Field_K=b.Field_K  

oracle中left join和right join的区别 - anly_liu - anly的博客   

oracle中left join和right join的区别 - anly_liu - anly的博客   Field_K          Field_A          Field_K          Field_B          

oracle中left join和right join的区别 - anly_liu - anly的博客  ----------    ----------    ----------    ----------    

oracle中left join和right join的区别 - anly_liu - anly的博客  1                      a                     1                      x                    

oracle中left join和right join的区别 - anly_liu - anly的博客  NULL               NULL               2                      y                    

oracle中left join和right join的区别 - anly_liu - anly的博客  4                      c                     4                      z      --

4,例子

 

假设a表和b表的数据是这样的。  

oracle中left join和right join的区别 - anly_liu - anly的博客   a                          b    

oracle中left join和right join的区别 - anly_liu - anly的博客   id      name  id      stock   

oracle中left join和right join的区别 - anly_liu - anly的博客  1  a             1         15  

oracle中left join和right join的区别 - anly_liu - anly的博客  2          b             2         50  

oracle中left join和right join的区别 - anly_liu - anly的博客  3          c      

 

 select   *   from    a   inner   join    b   on    a.id=b.id  

oracle中left join和right join的区别 - anly_liu - anly的博客   这个语法是连接查询中的内连接,它产生的结果是  

oracle中left join和right join的区别 - anly_liu - anly的博客   两个表相匹配的记录出现在结果列表中。  

oracle中left join和right join的区别 - anly_liu - anly的博客   根据上面的表,出现的结果是这样的  

oracle中left join和right join的区别 - anly_liu - anly的博客   a.id      name      b.id      stock  

oracle中left join和right join的区别 - anly_liu - anly的博客  1        a             1         15  

oracle中left join和right join的区别 - anly_liu - anly的博客  2              b             2         50  

oracle中left join和right join的区别 - anly_liu - anly的博客  ----------------------------  

oracle中left join和right join的区别 - anly_liu - anly的博客  select   *   from    a,b   where    a.id=b.id  

oracle中left join和right join的区别 - anly_liu - anly的博客   这个语法是内连接的另外一种写法,其执行结果与inner   join    一样  

------------------------------------------------------------

 

 select   *   from    a   left/right   join    b   on    a.id=b.id  

oracle中left join和right join的区别 - anly_liu - anly的博客   这个是外连接语法中的左外连接或右外连接  

oracle中left join和right join的区别 - anly_liu - anly的博客   如果是左外连接的话,它将显示a表的所有记录,  

oracle中left join和right join的区别 - anly_liu - anly的博客  select    a.*,b.*   from    a   left   join    b   on    a.id=b.id  

oracle中left join和right join的区别 - anly_liu - anly的博客   查询的结果是这样的:  

oracle中left join和right join的区别 - anly_liu - anly的博客   a.id      name      b.id      stock  

oracle中left join和right join的区别 - anly_liu - anly的博客  1          a         1             15  

oracle中left join和right join的区别 - anly_liu - anly的博客  2                b         2             50  

oracle中left join和right join的区别 - anly_liu - anly的博客  3                c       null         null   

oracle中left join和right join的区别 - anly_liu - anly的博客  --------------------------------------------  

oracle中left join和right join的区别 - anly_liu - anly的博客   如果是右外连接的话,它将显示b表的所有记录,  

oracle中left join和right join的区别 - anly_liu - anly的博客  select    a.*,b.*   from    a   right   join    b   on    a.id=b.id  

oracle中left join和right join的区别 - anly_liu - anly的博客   查询的结果是这样的:  

oracle中left join和right join的区别 - anly_liu - anly的博客   a.id      name      b.id      stock  

oracle中left join和right join的区别 - anly_liu - anly的博客  1          a         1             15  

oracle中left join和right join的区别 - anly_liu - anly的博客  2                b         2             50   

 

select    a.*,b.*   from    a   left   join    b   on    a.k   =    b.k    

select    a.*,b.*   from    a   left   outer   join    b   on    a.k   =b.k 

  

oracle中left join和right join的区别 - anly_liu - anly的博客  ----------上面两种一样left    join是left    outer    join的简写  

oracle中left join和right join的区别 - anly_liu - anly的博客  select    a.*,b.*   from    a   left   inner   join    b   on    a.k   =    b.k    

oracle中left join和right join的区别 - anly_liu - anly的博客   没有这种写法,错误的语句.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(sql)