oracle之子查询、连接查询、递归查询

一、子查询

  Oracle里面子查询:单行子查询 多行子查询 多列子查询 内联视图子查询。

  1、单行单列子查询。

--查询与员工SMITH属于同一个部门的员工
select * from emp where deptno=(select deptno from emp where ename='SMITH')

  2、多行子查询

--查询工作岗位与部门10的工作岗位相同的员工信息 
select * from emp where job in(select job from emp  where deptno=10) and deptno!=10;
--使用exists 存在 岗位相同
select *
  from emp e1
 where exists (select job
          from emp e2
         where deptno = 10
         and e1.job = e2.job
           )
and deptno != 10
--3.查询工作岗位与部门10的工作岗位不相同的员工信息 
select * from emp where job not in(select job from emp where deptno=10)
--使用not exists 存在 岗位不相同
select *
  from emp e1
 where not exists (select job
          from emp e2
         where deptno = 10
         and e1.job = e2.job
           )

  3、多列子查询。

--查询跟smith同一个部门,同一个岗位的员工信息
select * from emp where 
deptno=(select deptno from emp where ename='SMITH') 
AND 
 job=(select JOB from emp where ename='SMITH')
 -- 
 select *
   from emp
  where (deptno, job) = (select deptno,job from emp where ename = 'SMITH'

  4、内联视图子查询 伪表

--将部门30中工资排序前5位员工信息
 select rownum, e.*
   from (select * from emp where deptno = 30 order by sal desc) e
  where rownum <= 5
--将查询所有员工工资排序在6-10位之间的员工信息 每页显示五条。 1-5 6-10
 -- 当前页数pageNo 每页的数据为5条  pageSize
 --开始条书>:0  5   10   (pageNo-1)*pageSize
 --结束条数<=: 5  10  15  pageNo*pageSize  
 --rownum 每个结果集的序号
 select * from (
   select rownum rn, e.*
     from (select * from emp order by sal desc) e
    where rownum <= 2*5 ) e1  where e1.rn>2-1*5

  5、子查询中All关键字的使用

--显示工资比部门30的所有员工的工资都要高的员工信息
--第一种实现方式
select * from emp where sal>(select max(sal) from emp where deptno=30)
--第二种 sal要大于结果集中所有的记录
select * from emp where  sal>all(select sal from emp where deptno=30)

  6、子查询中any关键字的使用

--显示工资比部门30的任意员工的工资高的员工信息
   select * from emp where sal>(select min(sal) from emp where deptno=30) 
select * from emp where sal>any(select sal from emp where deptno=30)

  7、笛卡尔积

--显示10部门下所有员工的部门名称
   select d.dname,e.ename,e.sal from emp e,dept d    --得到dept和emp数据的笛卡尔积
   where e.deptno=d.deptno and e.deptno=10--通过where条件去除笛卡尔积

--exam:查询工资高于自己部门平均工资的员工的姓名和工资信息
   --先找出每个部门的平均工资
   select ename,sal from emp e1,
   (select deptno,avg(sal) dept_sal from emp group by deptno) avge
   where e1.deptno = avge.deptno and e1.sal>avge.dept_sal

  二、连接查询:左连接 右连接 等值连接

  连接运算:连接运算是有一个笛卡尔积运算和一个选取运算构成的。首先用笛卡尔积完成结果集的乘运算再对生成的结果集做选取运算确保只把分别来自两个数据集合并切有重叠的部分的合并在一起 。

  1、左外连接

select s.stuname,se.examsubject,se.examresult from studentinfo s 
left join
STUDENTEXAM se on s.stuid = se.estuid

  2、右外连接

select s.stuname,se.examsubject,se.examresult from studentinfo s 
right join
STUDENTEXAM se on s.stuid = se.estuid

  3、内连接

select s.stuname, se.examsubject, se.examresult
  from studentinfo s
 inner join studentexam se
    on s.stuid = se.estuid
--和内连接等同
select s.stuname,se.examsubject,se.examresult from studentinfo s 
,STUDENTEXAM se where  se.estuid = s.stuid

  4、自连接

--查询员工smith的上级领导的姓名
select a.ENAME, A.MGR, b.ENAME
  from emp a, emp b
 where a.mgr = b.EMPNO
   and a.Ename = 'SMITH';
   select * from emp

  注意:当你写连接查询的时候一定要考虑业务需求。

三、递归查询。

  递归查询是oracle中特有的查询,使用关键字START WITH ……CONNECT BY ……prior

--员工编号,上级的员工编号
   select * from emp start with empno=7839 connect by   mgr=prior empno;

 

posted on 2015-10-19 22:31  阡陌丶  阅读(762)  评论(0编辑  收藏  举报

导航