ORCALE左右连接

 

Sql代码 复制代码
  1. --建立测试数据   
  2. create table a(id number);   
  3. create table b(id number);   
  4. insert into a values(1);   
  5. insert into a values(2);   
  6. insert into a values(3);   
  7. insert into b values(1);   
  8. insert into b values(2);   
  9. insert into b values(4);   
  10. commit;   
  11.   
  12. --左:   
  13. --主流数据库通用的方法   
  14. select * from a left join b on a.id=b.id;   
  15. --Oracle特有的方法   
  16. select * from a, b where a.id=b.id(+);   
  17.   
  18.         ID         ID   
  19. ---------- ----------   
  20.          1          1   
  21.          2          2   
  22.          3    
  23.   
  24.   
  25. --右:   
  26. --主流数据库通用的方法   
  27. select * from a right join b on a.id=b.id;   
  28. --Oracle特有的方法   
  29. select * from a, b where a.id(+)=b.id;   
  30.   
  31.         ID         ID   
  32. ---------- ----------   
  33.          1          1   
  34.          2          2   
  35.                     4   
  36.             
  37.             
  38. --内   
  39. --主流数据库通用的方法   
  40. select * from a join b on a.id=b.id;   
  41. --where关联   
  42. select * from a, b where a.id=b.id;   
  43.   
  44.         ID         ID   
  45. ---------- ----------   
  46.          1          1   
  47.          2          2   
  48.             
  49.             
  50. --全外   
  51. --主流数据库通用的方法   
  52. select * from a full join b on a.id=b.id;   
  53. --Oracle特有的方法   
  54. select *   
  55.   from a, b   
  56.  where a.id = b.id(+)   
  57. union  
  58. select *    
  59.   from a, b    
  60.  where a.id(+) = b.id;   
  61.   
  62.         ID         ID   
  63. ---------- ----------   
  64.          1          1   
  65.          2          2   
  66.          3    
  67.                     4   
  68.   
  69.   
  70. --完全,也叫交叉连接或者笛卡尔积   
  71. --主流数据库通用的方法   
  72. select * from a,b;   
  73. --或者   
  74. select * from a cross join b;   
  75.   
  76.         ID         ID   
  77. ---------- ----------   
  78.          1          1   
  79.          1          2   
  80.          1          4   
  81.          2          1   
  82.          2          2   
  83.          2          4   
  84.          3          1   
  85.          3          2   
  86.          3          4   
  87.   
  88.   
  89. 连接无非是这几个   
  90. --内连接和where相同   
  91. inner join  
  92. --左向外连接,返回左边表所有符合条件的   
  93. left join  
  94. --右向外连接,返回右边表所有符合条件的   
  95. right join  
  96. --完整外部连接,左向外连接和右向外连接的合集   
  97. full join  
  98. --交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合   
  99. cross join  
  100.   
  101.   
  102. --补充:   
  103. --左向外连接,返回左边表所有符合条件的,   
  104. --注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录   
  105. select *    
  106.   from a, b   
  107.  where a.id = b.id(+)   
  108.    and b.id = 2;   
  109.       
  110.         ID         ID   
  111. ---------- ----------   
  112.          2          2      
  113.             
  114.             
  115. --左向外连接,返回左边表所有符合条件的   
  116. --注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null   
  117. select *   
  118.   from a, b   
  119.  where a.id = b.id(+)   
  120.    and b.id(+) = 2;   
  121.   
  122.         ID         ID   
  123. ---------- ----------   
  124.          2          2   
  125.          3    
  126.          1       
  127.            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值