1 . 自连接
解释 : 自连接是指在同一张表的连接查询 。
例子 : select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno and worker.ename='FORD';
子查询 : 指的就是嵌套查询 ;
例子:select ENAME,DEPTNO from emp where deptno = (select DEPTNO from emp where ename = 'SMITH');
子查询包含单行子查询(第二个select语句返回一个结果) 和多行子查询(返回多个结果) ;
在子查询中使用all关键字;
select * from emp where sal > all(select sal from emp where deptno = 30);
在子查询中使用any关键字;
select * from emp where sal > any(select sal from emp where deptno = 30);
多列子查询:
select * from emp where (deptno,job) = (select deptno,job from emp where ename='SMITH' );
复杂的查询:
select * from emp a1,(select deptno,avg(sal) mysal from emp group by deptno) a2
where a1.deptno=a2.deptno and a1.sal>a2.mysal;
记住给表区别名的时候不能使用as关键字 但是列可以 加as
-----------------------------------------------------------------------------------------
oracle的分页 一共有三种方式 :
假如我要查询 数据库中 5 - 10 的记录
第一步: 使用rownum 分页:
select emp1.* ,rownum from (select * from emp) emp1;
其中的 rownum 是oracle本身分配的;
第二步:select emp1.* ,rownum from (select * from emp) emp1 where rownum<=10;
第三步 :
select * from (select emp1.* ,rownum rn from (select * from emp) emp1 where rownum<=10) emp2 where rn>=5;
一定要记得 给 rownum 取别名; 要不然是查不出数据的;
如果要对某个字段进行排序 只需要改动 最底层的select 语句select * from emp 只需改这个语句:
例如 : 我们要对emp表的sal 进行降序排
select * from (select emp1.* ,rownum rn from (select sal,ename from emp order by sal desc) emp1 where rownum<=10) emp2 where rn>=5;
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
怎样用查询结果去创建表,这种命令是一种快捷的建表方法:
create table mytable(id,name,sal,job,deptno) as select empno,ename,sal,job,deptno from emp;
集合操作符为了合并 多个select语句的查询出来的结果
union (联合)
该操作符用于取得 两个结果集的并集 。当使用该操作符时, 会自动去掉重复结果集中的重复行
intersect(交集)
select * from emp where sal >1500 intersect select * from emp where JOB = 'MANAGER';
minus(差集)
select * from emp where sal >1500 minus select * from emp where JOB = 'MANAGER';