zoukankan      html  css  js  c++  java
  • 数据库左右连接(分享)

    • --建立测试数据 
    • createtable a(id number); 
    • createtable b(id number); 
    • insertinto a values(1); 
    • insertinto a values(2); 
    • insertinto a values(3); 
    • insertinto b values(1); 
    • insertinto b values(2); 
    • insertinto b values(4); 
    • commit
    •  
    • --左: 
    • --主流数据库通用的方法 
    • select * from a leftjoin 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 rightjoin 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 fulljoin 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 crossjoin b; 
    •  
    •         ID         ID 
    • ---------- ---------- 
    •          1          1 
    •          1          2 
    •          1          4 
    •          2          1 
    •          2          2 
    •          2          4 
    •          3          1 
    •          3          2 
    •          3          4 
    •  
    •  
    • 连接无非是这几个 
    • --内连接和where相同 
    • innerjoin 
    • --左向外连接,返回左边表所有符合条件的 
    • leftjoin 
    • --右向外连接,返回右边表所有符合条件的 
    • rightjoin 
    • --完整外部连接,左向外连接和右向外连接的合集 
    • fulljoin 
    • --交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合 
    • crossjoin 
    •  
    •  
    • --补充: 
    • --左向外连接,返回左边表所有符合条件的, 
    • --注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录 
    • 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     
    •           
  • 相关阅读:
    chrome浏览页面常用快捷键 && 常见的HTTP状态码
    hasCode详解
    队例初始化问题(Queue)
    新手问题--双链表最后一个节点无法删除问题
    高级排序--快速排序
    高级排序--归并排序
    高级排序--希尔排序
    简单排序--插入排序
    简单排序--选择排序
    简单排序--冒泡排序
  • 原文地址:https://www.cnblogs.com/haoxin963/p/2649452.html
Copyright © 2011-2022 走看看