Oracle 学习笔记4
来源:互联网 发布:淘宝开店装修流程 编辑:程序博客网 时间:2024/05/21 17:45
九 子查询
子查询:指嵌入在其他sql语句中的select语句,也叫嵌套查询。
单行子查询:指只返回一行数据的子查询语句。
eg:显示与SMITH同意部门的所有员工:
步骤1:查出SMITH的部门号
SQL>select deptno from emp where ename = 'SMITH';
步骤2:显示该部门的所有员工:
SQL>select * from emp where deptno =(select deptno from emp where ename='SMITH');
数据库在执行sql语句的顺序是:从左到右。
多行查询:返回多行数据的子查询。
eg:如何查询和部门10的工作相同的雇员的名字,岗位,工资,部门号:
SQL>select * from emp where job in(select distinct job from emp where deptno=10);
多行子查询中使用all操作符:
eg:显示工资比部门30的所有员工的工资高的员工的姓名,工资及部门号。
SQL>select ename,sal,deptno from emp where sal>all (select sal from emp where deptno=30);
方法2:
SQL>select * from emp where sal>(select max(sal) from emp where deptno=30);
any操作符:
eg:显示工资比部门30 的任意一个员工的工资高的员工姓名,工资,部门号:
SQL>select ename ,sal,dept from emp where sal>any(select sal from emp where deptno=30);
多列子查询:
eg:查询与SMITH的部门和岗位完全相同的雇员:
步骤1:查出smith的部门号,岗位
SQL>select deptno ,job from emp where ename ='SMITH';
SQL>select * from emp where (deptno ,job )=(select deptno,job from emp where ename ='SMITH');
from 子句中使用子查询:
eg:如何显示高于自己部门平均工资员工的信息:
SQL>select a2.ename ,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno ,avg(sal) mysal from emp group by deptno )a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;
这里需要说明的是,当在from子句中使用子查询时,该子查询会被作为一个视图来对待,因此叫做内嵌视图,当在from子句中使用子查询时,必须给子查询指定别名。
注意:列名可以加as,表名不可以加as
10 分页查询
oracle的分页一共有三种方式:
1 rownum分页
SQL>select a1.*,rownum rn from (select * from emp) a1;
步骤 1
SQL>select * from emp ;
SQL>select a1.*,rownum rn from (select * from emp)a1 where rownum <=10;
SQL>select * from(select a1.*,rownum rn from (select * from emp)a1 where rownum <=10) where rn>=6;
几个查询的变化:
a:指定查询列,只需修改最里层的子查询。
b:如何排序,只需修改最内层的子查询。
SQL>select count(*) from emp ;这个语句是查询有多少行。
如何快捷建立一个表:
SQL>create table mytable (id,name,sal,job,deptno) as select empno,sal,job,deptno from emp;
合并查询:为了合并多个select语句结果,可以使用操作符union,unionall,intersect,minus.
union用于取得两个结果集的并集,当使用操作符,会自动去掉结果集中的重复行。
SQL>select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER';
unionall与union相似,但是它不会取消重复行,而且不排序。
intersect 用于去交集。
SQL>select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job='MANAGER';
minus用于取两个结果的差集。
SQL>select ename ,sal,job from emp where sal>2500 minus select ename ,sal,job from emp where job='MANAGER';
子查询:指嵌入在其他sql语句中的select语句,也叫嵌套查询。
单行子查询:指只返回一行数据的子查询语句。
eg:显示与SMITH同意部门的所有员工:
步骤1:查出SMITH的部门号
SQL>select deptno from emp where ename = 'SMITH';
步骤2:显示该部门的所有员工:
SQL>select * from emp where deptno =(select deptno from emp where ename='SMITH');
数据库在执行sql语句的顺序是:从左到右。
多行查询:返回多行数据的子查询。
eg:如何查询和部门10的工作相同的雇员的名字,岗位,工资,部门号:
SQL>select * from emp where job in(select distinct job from emp where deptno=10);
多行子查询中使用all操作符:
eg:显示工资比部门30的所有员工的工资高的员工的姓名,工资及部门号。
SQL>select ename,sal,deptno from emp where sal>all (select sal from emp where deptno=30);
方法2:
SQL>select * from emp where sal>(select max(sal) from emp where deptno=30);
any操作符:
eg:显示工资比部门30 的任意一个员工的工资高的员工姓名,工资,部门号:
SQL>select ename ,sal,dept from emp where sal>any(select sal from emp where deptno=30);
多列子查询:
eg:查询与SMITH的部门和岗位完全相同的雇员:
步骤1:查出smith的部门号,岗位
SQL>select deptno ,job from emp where ename ='SMITH';
SQL>select * from emp where (deptno ,job )=(select deptno,job from emp where ename ='SMITH');
from 子句中使用子查询:
eg:如何显示高于自己部门平均工资员工的信息:
SQL>select a2.ename ,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno ,avg(sal) mysal from emp group by deptno )a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;
这里需要说明的是,当在from子句中使用子查询时,该子查询会被作为一个视图来对待,因此叫做内嵌视图,当在from子句中使用子查询时,必须给子查询指定别名。
注意:列名可以加as,表名不可以加as
10 分页查询
oracle的分页一共有三种方式:
1 rownum分页
SQL>select a1.*,rownum rn from (select * from emp) a1;
步骤 1
SQL>select * from emp ;
SQL>select a1.*,rownum rn from (select * from emp)a1 where rownum <=10;
SQL>select * from(select a1.*,rownum rn from (select * from emp)a1 where rownum <=10) where rn>=6;
几个查询的变化:
a:指定查询列,只需修改最里层的子查询。
b:如何排序,只需修改最内层的子查询。
SQL>select count(*) from emp ;这个语句是查询有多少行。
如何快捷建立一个表:
SQL>create table mytable (id,name,sal,job,deptno) as select empno,sal,job,deptno from emp;
合并查询:为了合并多个select语句结果,可以使用操作符union,unionall,intersect,minus.
union用于取得两个结果集的并集,当使用操作符,会自动去掉结果集中的重复行。
SQL>select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER';
unionall与union相似,但是它不会取消重复行,而且不排序。
intersect 用于去交集。
SQL>select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job='MANAGER';
minus用于取两个结果的差集。
SQL>select ename ,sal,job from emp where sal>2500 minus select ename ,sal,job from emp where job='MANAGER';
- oracle学习笔记4
- Oracle 学习笔记4
- oracle学习笔记4
- Oracle学习笔记(4)
- Oracle学习笔记--Oracle入门
- 自增字段 - Oracle 学习笔记 4
- oracle学习笔记4:性能调优
- 北大青鸟oracle学习笔记4 5
- oracle 学习笔记2011-4-1
- oracle学习笔记-2010-4-5
- Oracle基础学习笔记(4)
- Oracle基础学习笔记(4)
- Oracle学习笔记4-游标的基本概念
- Oracle学习笔记 4 --单行函数
- Oracle学习笔记(4)------------简单查询
- Oracle学习笔记(4)------------简单查询
- oracle学习笔记-存储空间管理(4)
- Oracle学习笔记
- Binary search and so forth
- 把自己的jsp或html模板添加到myeclipse中
- Oracle 学习笔记 1
- Oracle 学习笔记2
- Oracle 学习笔记3
- Oracle 学习笔记4
- Oracle学习笔记 5
- Oracle 学习笔记6
- Oracle 学习笔记7
- 数据挖掘之距离计算算法
- java与c++的区别
- cocos2d-x系列笔记(3)---场景
- 质量块-阻尼器-弹簧系统的鲁棒控制
- CAN BAUD设置详解