oracle 多表查詢

來源:互聯網
上載者:User

標籤:logs   首碼   out   角度   from   dep   不同   自串連   between   

1、等值串連:取關係列相同的記錄
select * from emp e,dept d where e.deptno=d.deptno;
查出emp、dept表中deptno列相同的記錄

2、非等值串連:取關係列不相同的記錄
select * from emp e,dept d where e.deptno>d.deptno;
查出emp表中deptno大於dept表中的記錄
3、外串連:左左外串連以左表為主,右外串連以右表為主
右串連,以右表為主,右表所有記錄多查詢出來,左表只查出複合條件的記錄
select * from emp e,dept d where e.deptno(+)=d.deptno;
select * from emp e right outer join dept d on e.deptno=d.deptno;

左串連,以左表為主,左表所有記錄多查詢出來,右表只查出複合條件的記錄
select * from emp e,dept d where e.deptno=d.deptno(+);
select * from emp e left outer join dept d on e.deptno=d.deptno;

全串連 ,所有記錄來自於兩張表,用null值來匹配缺失值
select * from emp e full join dept d on e.deptno=d.deptno;

4、自串連:特殊等值串連,取同一張表不同角度看待的記錄
select e1.ename || ‘ 領導是‘ || e2.ename from emp e1, emp e2 where e1.mgr = e2.empno;

5、交叉串連產生一個笛卡爾積 cross join
笛卡爾積:多表查詢時,查詢記錄的行數等於所有表行數的乘積,列數等於所有表列數之和
select * from emp ,dept;
select * from emp cross join dept;

6、自然串連 natural join (注)所有相同列等值串連
natural join 子句是基於兩個表中列名完全相同的列產生串連,查詢串連列的值相等的記錄,不保留重複的屬性。
natural join 子句中的列不能使用表別名做首碼。如:e.deptno=‘10‘ 或 d.deptno=‘10‘

查出兩表中相同列值相同的記錄
select * from emp e natural join dept d;

查出兩表中指定列值相同的記錄
select * from emp e natural join dept d where deptno=‘10‘;

7、using(X)建立串連
using子句引用的列在sql任何地方都不能使用表名或別名做首碼

查出兩表中相同列值相同的記錄
select * from emp e join dept d using(deptno);

查出兩表中指定列值相同的記錄
select * from emp e join dept d using(deptno) where deptno=‘30‘;

8、join on 建立串連 ,不同於自然串連,使用join on 可以指定串連列和設定任意的串連條件

select * from emp e join dept d on e.deptno=d.deptno where e.deptno=‘10‘;

join on 可以進行兩張以上的表串連查詢
select * from emp e join dept d on e.deptno=d.deptno join salgrade s on e.sal between s.losal and s.hisal where e.deptno=‘10‘;

 

oracle 多表查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.