• 文字广告位
  • 文字广告位
  • 文字广告位
您现在的位置: 站长手册 >> 数据库 >> ORACLE >> 正文
站内搜索
Google
[组图]用SQL进行嵌套查询
         【字体:

用SQL进行嵌套查询

雅虎收藏夹 百度收藏 Google书签 Yahoo书签 新浪ViVi 搜狐网摘 365Key网摘 天极网摘 diglog 和讯网摘 POCO网摘 YouNote网摘 博拉网 天下图摘 Del.icio.us digg reddit spurl BlinkList blogmarks
人气: 来源:网络 作者:佚名 所属栏目:ORACLE [切换到繁體中文]
  在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.4\441.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.4\442.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.4\443.sql。

    带any的查询过程等价于两步的执行过程。
    (1)执行“select sal from scott.emp where job='MANAGER'”,其结果如图4.22所示。
    【参见光盘文件】:\第4章\4.4\443-1.sql。

    (2)查询到3个薪水值2975、2850和2450,父查询执行下列语句。
    【参见光盘文件】:\第4章\4.4\443-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.4\444.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.4\444-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.4\445.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.4\445-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.4\446.sql。

4.4.7 并操作的嵌套查询
    并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。 在【命令编辑区】执行下列语句。
    —————————————————————————————————————
     (select deptno from scott.emp) union (select deptno from scott.dept);     —————————————————————————————————————
    单击【执行】按钮,出现如图4.26所示的结果。
    【参见光盘文件】:\第4章\4.4\447.sql。

4.4.8 交操作的嵌套查询
    交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。 在【命令编辑区】执行下列语句。
    —————————————————————————————————————
    (select deptno from scott.emp) intersect (select deptno from scott.dept);     —————————————————————————————————————
    单击【执行】按钮,出现如图4.27所示的结果。
    【参见光盘文件】:\第4章\4.4\448.sql。

4.4.9 差操作的嵌套查询
    差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。
    在【命令编辑区】执行下列语句。
    —————————————————————————————————————
    (select deptno from scott.dept) minus (select deptno from scott.emp);
    —————————————————————————————————————
    单击【执行】按钮,出现如图4.28所示的结果。
    【参见光盘文件】:\第4章\4.4\449.sql。

    并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。
  • 上一篇文章:
  • 下一篇文章:
  • 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    姓 名: *(必填项) ·注册用户·
    Email: QQ号:
    评 分: 1分 2分 3分 4分 5分
    • 您可以发表评论支持你喜欢的文章。
    • 请遵守《互联网电子公告服务管理规定》
    • 请遵守中华人民共和国各项有关法律法规。
    • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
    • 评论人需对自己在使用评论过程中的行为承担法律责任。
    • 本站管理员有权保留或删除评论内容。
    • 评论内容只代表个人观点,与本网站立场无关。
    站内文章搜索
    ·最新文章      
    ·热门文章      
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | | 京ICP备 05004866号 |
    版权所有 2007-2008 站长手册 WWW.ZZSC.ORG 业务联系 zzsc.org#gmail.com