Oracle左右全连接总结

--建立测试数据   
create table a(id number);   
create table b(id number);   
insert into a values(1);   
insert into a values(2);   
insert into a values(3);   
insert into b values(1);   
insert into b values(2);   
insert into b values(4);   
commit;   
  
--左:   
--主流数据库通用的方法   
select * from a left join b on a.id=b.id;   
--Oracle特有的方法   
select * from a, b where a.id=b.id(+);   
  
        ID         ID   
---------- ----------   
         1          1   
         2          2   
         3    
  
  
--右:   
--主流数据库通用的方法   
select * from a right join b on a.id=b.id;   
--Oracle特有的方法   
select * from a, b where a.id(+)=b.id;   
  
        ID         ID   
---------- ----------   
         1          1   
         2          2   
                    4   
            
            
--内   
--主流数据库通用的方法   
select * from a join b on a.id=b.id;   
--where关联   
select * from a, b where a.id=b.id;   
  
        ID         ID   
---------- ----------   
         1          1   
         2          2   
            
            
--全外   
--主流数据库通用的方法   
select * from a full join b on a.id=b.id;   
--Oracle特有的方法   
select *   
  from a, b   
 where a.id = b.id(+)   
union   
select *    
  from a, b    
 where a.id(+) = b.id;   
  
        ID         ID   
---------- ----------   
         1          1   
         2          2   
         3    
                    4   
  
  
--完全,也叫交叉连接或者笛卡尔积   
--主流数据库通用的方法   
select * from a,b;   
--或者   
select * from a cross join b;   
  
        ID         ID   
---------- ----------   
         1          1   
         1          2   
         1          4   
         2          1   
         2          2   
         2          4   
         3          1   
         3          2   
         3          4   
  
  
连接无非是这几个   
--内连接和where相同   
inner join   
--左向外连接,返回左边表所有符合条件的   
left join   
--右向外连接,返回右边表所有符合条件的   
right join   
--完整外部连接,左向外连接和右向外连接的合集   
full join   
--交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合   
cross join   
  
  
--补充:   
--左向外连接,返回左边表所有符合条件的,   
--注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录   
select *    
  from a, b   
 where a.id = b.id(+)   
   and b.id = 2;   
      
        ID         ID   
---------- ----------   
         2          2      
            
            
--左向外连接,返回左边表所有符合条件的   
--注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null  
select *   
  from a, b   
 where a.id = b.id(+)   
   and b.id(+) = 2;   
  
        ID         ID   
---------- ----------   
         2          2   
         3    
         1       
           
--建立测试数据
create table a(id number);
create table b(id number);
insert into a values(1);
insert into a values(2);
insert into a values(3);
insert into b values(1);
insert into b values(2);
insert into b values(4);
commit;

--左:
--主流数据库通用的方法
select * from a left join b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id=b.id(+);

        ID         ID
---------- ----------
         1          1
         2          2
         3


--右:
--主流数据库通用的方法
select * from a right join b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id(+)=b.id;

        ID         ID
---------- ----------
         1          1
         2          2
                    4
         
         
--内
--主流数据库通用的方法
select * from a join b on a.id=b.id;
--where关联
select * from a, b where a.id=b.id;

        ID         ID
---------- ----------
         1          1
         2          2
         
         
--全外
--主流数据库通用的方法
select * from a full join b on a.id=b.id;
--Oracle特有的方法
select *
  from a, b
 where a.id = b.id(+)
union
select * 
  from a, b 
 where a.id(+) = b.id;

        ID         ID
---------- ----------
         1          1
         2          2
         3 
                    4


--完全,也叫交叉连接或者笛卡尔积
--主流数据库通用的方法
select * from a,b;
--或者
select * from a cross join b;

        ID         ID
---------- ----------
         1          1
         1          2
         1          4
         2          1
         2          2
         2          4
         3          1
         3          2
         3          4


连接无非是这几个
--内连接和where相同
inner join
--左向外连接,返回左边表所有符合条件的
left join
--右向外连接,返回右边表所有符合条件的
right join
--完整外部连接,左向外连接和右向外连接的合集
full join
--交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合
cross join


--补充:
--左向外连接,返回左边表所有符合条件的,
--注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录
select * 
  from a, b
 where a.id = b.id(+)
   and b.id = 2;
   
        ID         ID
---------- ----------
         2          2   
         
         
--左向外连接,返回左边表所有符合条件的
--注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null
select *
  from a, b
 where a.id = b.id(+)
   and b.id(+) = 2;

        ID         ID
---------- ----------
         2          2
         3 
         1    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值