当前位置:数据库 > Oracle >>

用SQL进行嵌套查询

答案:

  在select查询语句里可以嵌入select查询语句,称为嵌套查询。有些书上将内嵌的select语句称为子查询,子查询形成的结果又成为父查询的条件。

  子查询可以嵌套多层,子查询操作的数据表可以是父查询不操作的数据表。子查询中不能有order by分组语句。

  4.4.1 简单嵌套查询

  在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>=(select sal from scott.emp where ename='WARD');

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.19所示的结果。

  【参见光盘文件】:第4章4.4441.sql。

  在这段代码中,子查询select sal from scott.emp where ename='WARD'的含义是从emp数据表中查询姓名为WARD的员工的薪水,父查询的含义是要找出emp数据表中薪水大于等于WARD的薪水的员工。上面的查询过程等价于两步的执行过程。

  (1)执行“select sal from scott.emp where ename='WARD'”,得出sal=1250;

  (2)执行“select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>=1250;”

  4.4.2 带【in】的嵌套查询

  在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where

  ename='WARD');

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.20所示的结果。

  

  【参见光盘文件】:第4章4.4442.sql。

  上述语句完成的是查询薪水和WARD相等的员工,也可以使用【not in】来进行查询。 4.4.3 带【any】的嵌套查询

  在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where

  job='MANAGER');

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.21所示的结果。

  【参见光盘文件】:第4章4.4443.sql。

  带any的查询过程等价于两步的执行过程。

  (1)执行“select sal from scott.emp where job='MANAGER'”,其结果如图4.22所示。

  【参见光盘文件】:第4章4.4443-1.sql。

  (2)查询到3个薪水值2975、2850和2450,父查询执行下列语句。

  

  【参见光盘文件】:第4章4.4443-2.sql。

  ??????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450;

  ??????????????????????????????????????

  4.4.4 带【some】的嵌套查询

  在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where

  job='MANAGER');

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.23所示的结果。

  【参见光盘文件】:第4章4.4444.sql。

  带some的嵌套查询与any的步骤相同。

  (1)子查询,执行“select sal from scott.emp where job='MANAGER'”,其结果如图4.22所示。

  (2)父查询执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450;

  ?????????????????????????????????????

  【参见光盘文件】:第4章4.4444-2.sql。

  带【any】的嵌套查询和【some】的嵌套查询功能是一样的。早期的SQL仅仅允许使用【any】,后来的版本为了和英语的【any】相区分,引入了【some】,同时还保留了【any】关键词。

  4.4.5 带【all】的嵌套查询

  在【命令编辑区】执行下列语句。

  

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >all(select sal from scott.emp where

  job='MANAGER');

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.24所示的结果。

  【参见光盘文件】:第4章4.4445.sql。

  带all的嵌套查询与【some】的步骤相同。

  (1)子查询,结果如图4.22所示。

  (2)父查询执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 and sal>2850 and sal>2450;

  ?????????????????????????????????????

  【参见光盘文件】:第4章4.4445-2.sql。

  4.4.6 带【exists】的嵌套查询

  在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept where exists (select * from scott.emp where

  scott.emp.deptno=scott.dept.deptno);

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.25所示的结果。

  【参见光盘文件】:第4章4.4446.sql。

  4.4.7 并操作的嵌套查询

  

  并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。 在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  (select deptno from scott.emp) union (select deptno from scott.dept);

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.26所示的结果。

  【参见光盘文件】:第4章4.4447.sql。

  4.4.8 交操作的嵌套查询

  交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。 在【命令编辑区】执行下列语句。

  ?????????????????????????????????????

  (select deptno from scott.emp) intersect (select deptno from scott.dept);

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.27所示的结果。

  【参见光盘文件】:第4章4.4448.sql。

  4.4.9 差操作的嵌套查询

  差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。

  在【命令编辑区】执行下列语句。

  

  ?????????????????????????????????????

  (select deptno from scott.dept) minus (select deptno from scott.emp);

  ?????????????????????????????????????

  单击【执行】按钮,出现如图4.28所示的结果。

  【参见光盘文件】:第4章4.4449.sql。

  并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。本文来自:http://doc.linuxpk.com/77957.html
发表您的高见!

上一个:安装管理客户机
下一个:用SQL进行函数查询

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,