原创:oracle 子查询介绍<七>

前几节课中我们渐渐的对oracle的知识,有了进一步的了解与掌握,随着时间的推移,在这节课程中我们来讲解SQL子查询的知识,希望通过授课中的内容对你有所帮助!
 

Java代码 复制代码  收藏代码
  1. 子查询概念 :当一个查询的结果是另一个查询的条件时,称之为子查询。   
  2. 使用子查询注意事项:   
  3.          子查询可以嵌套多层   
  4.          子查询需要圆括号()括起来   
  5. 子查询语法:   
  6. SELECT  select_list    
  7. FROM    table    
  8. WHERE   expr operator   
  9.             (SELECT select_list    
  10.                FROM     table);   
  11. ?   子查询 (内查询) 在主查询之前一次执行完成。   
  12. ?   子查询的结果被主查询使用 (外查询)。   
  13. 举例:查询员工的工资大于JONES的员工信息   
  14. 分析过程如下:   
  15. 首先:查询JONES的员工工资是多少 :结果2975  
  16. SQL> select sal from emp where ename='JONES';   
  17. 实际上我们要查询的是:薪资大于2975的员工的信息写法如下:   
  18. SQL> select * from emp where sal>2975;   
  19. //综合以上写出子查询的结果如下:   
  20. SQL> select * from emp where sal>(select sal from emp where ename='JONES');   
  21. 注意:   
  22. ?   子查询要包含在括号内。    
  23. ?   将子查询放在比较条件的右侧。   
  24.   
  25. 根据查询的结果(内部嵌套查询的结果)把子查询的类型分为单行子查询与多行子查询,   
  26.    注意:   
  27. ?   单行操作符对应单行子查询,多行操作符对应多行子查询。   
  28. 单行操作符   
  29. >、>=、 <、 <= 、<>、=   
  30. 举例:   
  31. //查询编号7876相同职位的员工信息 并且薪资大于编号为7521的薪资的员工信息   
  32. SQL> select * from emp where job=( select job from emp where empno=7876) and sal>( select sal from emp where empno=7521);   
  33. //子查询含有组函数   
  34. SQL> select * from emp where sal>(select avg(nvl(sal,0)) from emp);   
  35. //子查询含有having子句 查询部门的最小工资大于20号部门最小工资的部门号及最小工资数   
  36. SQL> select deptno,min(sal) from emp group by deptno having min(sal)>( select min(sal) from emp where deptno=20);   
  37.   
  38. 备注:子查询可以返回空行 没要查询到结果是可以的。   
  39.   
  40. 多行子查询   
  41. ?    返回多行。   
  42. ?   使用多行比较操作符。   
  43. 操作符如下图:   
  44. 操作符 描述   
  45. In  等于列表中的任何一个   
  46. Any 子查询返回的任意一个值比较 相同还有some   
  47. All 和子查询返回的所有值比较     
  48. Exists     
  49.   
  50. //查询薪水小于工作岗位CLERK的任何一个薪资的员工信息 并且不包含工作岗位为CLERK的员工信息   
  51. SQL> select * from emp where sal < any (select sal from emp where job='CLERK') and job<>'CLERK';   
  52. //all与所有值比较 >all 代表的是大于查询结果的最大值   
  53. SQL> select * from emp where sal > all (select sal from emp where job='CLERK') and job<>'CLERK';   
  54. //查询岗位与部门编号为10相同的员工信息 不包含自己。   
  55. SQL> select * from emp where job in(select job from emp where deptno=10) and deptno<>10;   
  56.   
  57. 只做思考?   
  58.   
  59. SQL> select * from emp where exists(select * from dept);   
  60.     
  61. EMPNO ENAME  JOB        MGR HIREDATE       SAL      COMM DEPTNO   
  62. ----- ---------- --------- ----- ----------- --------- --------- ------   
  63.  7369 SMITH   CLERK   7902 1980-12-17    800.00               20  
  64.  7499 ALLEN   SALESMAN  7698 1981-2-20   1600.00    300.00     30  
  65.  7521 WARD   SALESMAN  7698 1981-2-22   1250.00    500.00     30  
  66.  …..   
  67.     
  68. SQL> select * from emp where exists(select * from dept where deptno=80);   
  69.     
  70. EMPNO ENAME  JOB        MGR HIREDATE       SAL      COMM DEPTNO   
  71. ----- ---------- --------- ----- ----------- --------- --------- ------   
  72.   
  73.   
  74. 以上内容版权归redarmy_chen所有,如需转载请附带出处,如有疑问请发送邮件至redarmy_chen@qq.com  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
/app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:683: undefined reference to `GetHistParam(inpackage*, std::string&, std::string&, std::string&, std::string&, std::string&, std::string&, int&, std::string&, oracle::occi::Date&, int&, std::string&, oracle::occi::Date&, oracle::occi::Date&, oracle::occi::Date&, std::string&, std::string&, std::string&, std::string&, std::string&, std::string)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:714: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:808: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' bin/util.o: In function `process_xhx_file(std::string, std::string, std::string, int, CFileControl*)': /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:987: undefined reference to `GetHistParam(inpackage*, std::string&, std::string&, std::string&, std::string&, std::string&, std::string&, int&, std::string&, oracle::occi::Date&, int&, std::string&, oracle::occi::Date&, oracle::occi::Date&, oracle::occi::Date&, std::string&, std::string&, std::string&, std::string&, std::string&, std::string)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:1107: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' /app/smsprev4/src/NewCore/piliang_src_cluster_2023/plcb_2/src/util.cpp:1201: undefined reference to `updatePreCB(inpackage*, oracle::occi::Date, std::string, std::string, std::string&)' collect2: error: ld returned 1 exit status
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值