日期:2014-05-16  浏览次数:20497 次

Oracle表的连接(内连、外连......)

--笛卡尔集(Cross Join)
??????? Select * from emp,dept;


--等值连接(Equijoin)(Natural join..on) --------内连接
????? select empno, ename, sal, emp.deptno, dname from emp, dept
????? where emp.deptno = dept.deptno;


--非等值连接(Non-Equijoin)
????? select ename,empno,grade,sal,losal,hisal from emp,salgrade where sal between losal and hisal;
?
--自连接(Self join)
???????? select e.empno,e.ename,m.empno,m.ename from emp e,emp m where e.mgr = m.empno;?
?
?
--左外联接(Left Outer Join ))---外连接----返回右表全部和左表满足条件的部分?
???????? select s.sid,s.sname,s1.sid,s1.sname from student s,student1 s1 where s.sid=s1.sid(+);?
?
?????????select empno,ename,dname from emp left outer join dept on emp.deptno = dept.deptno;
?
--右外联接(Right Outer Join))---外连接?----返回左表全部和右表满足条件的部分?

???????? select s.sid,s.sname,s1.sid,s1.sname from student s,student1 s1 where s.sid(+)=s1.sid;
?
???????? select empno,ename,dname from emp right outer join dept on emp.deptno = dept.deptno;
?
?
--满外联接(Full Outer Join)---外连接 --------在等值连接的基础上将左表和右表的未匹配数据都加上
???????? select empno,ename,dname from emp full outer join dept on emp.deptno = dept.deptno;
?
--UNION:并集,所有的内容都查询,重复的显示一次
--UNION? ALL:并集,所有的内容都显示,包括重复的
--INTERSECT:交集:只显示重复的
--MINUS:差集:只显示对方没有的(跟顺序是有关系的)

?

eg:?
???????? CREATE? TABLE? emp20? AS? SELECT? *? FROM? emp? WHERE? deptno=20? ;?
?
--UNION:
???????? ?SELECT? *? FROM? emp? UNION? SELECT? *? FROM? emp20? ;??????? --使用此语句重复的内容不再显示

--UNION? ALL:
????????? SELECT? *? FROM? emp? UNION? ALL? SELECT? *? FROM? emp20? ; --重复的内容依然显示
?
--INTERSECT
????????? SELECT? *? FROM? emp? INTERSECT? SELECT? *? FROM? emp20? ;? --只显示了两个表中彼此重复的记录
?
--MINUS???
??????????SELECT? *? FROM? emp? MINUS? SELECT? *? FROM? emp20? ;???????? --只显示了两张表中的不同记录? 返回差异的记录

?