连接数据库的四种方式

2026-02-12 16:02:12

1、 1) 内连接   


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

  或者是:

 select a.*,b.*  from a,b where a.id=b.parent_id

    


  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2  

2、 2)左连接   


  select   a.*,b.*   from   a   left   join   b     on   a.id=b.parent_id       

  或者是

 select a.*,b.*  from a,b where a.id=b.parent_id(+)


  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  3   王武                  null   

3、 3) 右连接   


  select   a.*,b.*   from   a   right   join   b     on   a.id=b.parent_id  

  或者是

  select   a.*,b.*   from   a ,b  where   a.id(+)=b.parent_id  

     


  结果是     
  1   张3                   1     23     1   
  2   李四                  2     34     2   
  null                       3     34     4   

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
猜你喜欢