`
lihong11
  • 浏览: 450447 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

sql左右连接的作用

阅读更多
左连接显示左边全部的和右边与左边相同的  a.id = b.parent_id(+)  left join 表名 on  
右连接显示右边全部的和左边与右边相同的  a.id(+)= b.parent_id   right join 表名 on  

内连接是只显示满足条件的!  inner join 表名 on

完全连接是显示两表的所有记录,不满足条件的以空显示! full join 表名 on


例子:   

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

a表                       b表

id   name             id   job   parent_id   
             

1   张3                   1     23     1   
             

2   李四                 2     34     2   
             

3   王武                 3     34     4       
 

a.id同parent_id   存在关系   

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

1) 内连接   
  select   a.*,b.*   from   a   inner   join   b     on   a.id=b.parent_id       
 

结果是     
 

1   张3                   1     23     1   
 

2   李四                  2     34     2   
    
  

2)左连接   
  select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id       
 

结果是     
 

1   张3                   1     23     1   
 

2   李四                  2     34     2   
 

3   王武                  null   

 

 3) 右连接   
  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id       
 

结果是     
 

1   张3                   1     23     1   
 

2   李四                  2     34     2   
 

null                       3     34     4   
    
 

4) 完全连接   
  select   a.*,b.*   from   a   full   join   b     on   a.id=b.parent_id   

  结果是     
 

1   张3                  1     23     1   
 

2   李四                 2     34     2   
 

null                   3     34     4   
 

3   王武                 null


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics