SQL子查询全接触(2)
作者:dnawo 时间:2007-08-20 10:51:00
二、子查询类型
1.带in的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where ename=’’WARD’’);
上述语句完成的是查询薪水和WARD相等的员工,也可以使用not in来进行查询。
2.带any的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)执行“select sal from scott.emp where job=’’MANAGER’’”
(2)查询到3个薪水值2975、2850和2450,父查询执行下列语句:
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450;
3.带some的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’MANAGER’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450;
带【any】的嵌套查询和【some】的嵌套查询功能是一样的。早期的SQL仅仅允许使用【any】,后来的版本为了和英语的【any】相区分,引入了【some】,同时还保留了【any】关键词。
4.带all的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >all(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’MANAGER’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 and sal>2850 and sal>2450;
5.带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);
6.并操作的嵌套查询
并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。
(select deptno from scott.emp) union (select deptno from scott.dept);
7.交操作的嵌套查询
交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。
(select deptno from scott.emp) intersect (select deptno from scott.dept);
8.差操作的嵌套查询
差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。
(select deptno from scott.dept) minus (select deptno from scott.emp);
并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。
标签:sql,查询,子查询
0
投稿
猜你喜欢
Oracle 数据库 临时数据的处理方法
2009-07-02 11:48:00
JavaScript Memoization
2008-05-01 12:48:00
[精品]ASP中常用的22个FSO文件操作函数
2007-08-18 15:12:00
SQL点滴24 监测表的变化
2011-09-30 11:38:41
用YSlow评分插件分析我们页面
2008-08-26 11:48:00
使用 JScript 创建 .exe 或 .dll 文件
2011-06-04 15:37:00
SQL Server 2005五个动态管理对象
2009-02-24 17:41:00
css布局自适应高度方法
2007-05-11 17:03:00
WEB页面工具语言XML带来的好处
2008-05-29 11:01:00
表单系列·出错字段排行榜
2008-07-01 12:57:00
SQL Server 2005返回刚刚插入的数据条目id值
2008-12-04 17:16:00
基于PHP做个图片防盗链
2023-05-25 00:27:30
IE6下的CSS BUG枚举
2010-06-11 10:45:00
IE下img多余5像素空白
2009-06-08 13:17:00
SQLServer 游标简介与使用说明
2009-07-02 13:53:00
如何使div在任何分辩率的情况下居中
2007-08-13 09:10:00
web标准页面知识必备 Ⅰ
2008-03-06 13:24:00
SQL Server可写脚本和编程扩展SSIS包
2009-01-20 16:29:00
用ASP对网页进行限制性的访问
2008-07-03 13:02:00
JS的IE和FF兼容性问题汇总
2008-03-08 13:01:00