学习python第四天——Oracle查询
3.子查询(难): 当进行查询的时候,发现需要的数据信息不明确,需要先通过另一个查询得到, 此查询称为子查询; 执行顺序:先执行子查询得到结果以后返回给主查询 组成部分: 1).主查询部分 2).子查询部分 【注意事项】: 子查询一定需要被定义/包裹在小括号内部,可以认为是显示的提升了代码执行的优先级 需求1: 查询薪资比Abel的高的有谁? 分析: ①.先查询出Abel的薪资是多少? ②.将过滤条件定义为>①,然后进行查询得到最终需要的结果 代码实现: select last_name,salary from employees where salary > ( select salary from employees where last_name = 'Abel' ); 需求2: 查询job_id与141号员工相同,salary比143号员工多的员工的姓名,job_id和salary? 代码实现: select last_name,job_id,salary from employees where job_id = ( select job_id from employees where employee_id = 141 ) and salary > ( select salary from employees where employee_id = 143 ); 课堂练习: 1).返回公司工资最少的员工的employee_id,job_id和salary select employee_id,job_id,salary from employees where salary = ( select min(salary) from employees ); 2).查询平均工资高于公司平均工资的部门有哪些 select department_id,avg(salary) from employees group by department_id having avg(salary) > ( select avg(salary) from employees ) order by department_id desc; 3).查询最低工资大于20号部门最低工资的部门id和最低工资 select department_id,min(salary) from employees where department_id is not null group by department_id having min(salary) > ( select min(salary) from employees having department_id = 20 ); 4).返回其它职位中比job_id为'IT_PROG'中最低工资低的员工的员工号,姓名,job_id以及salary select employee_id,last_name,job_id,salary from employees where salary < ( select min(salary) from employees where job_id = 'IT_PROG' ); 4.多表查询/多表联查 概念: 使用场景,如果一条select语句中需要查询的列遍布多张数据表, 那么我们就必须使用多表查询了!! 分类: 等值连接和非等值连接 对于等值连接分方向: 1).内连接:返回多张表中共同满足的数据,取交集 2).外连接(左、右、满):返回内连接数据的同时还会继续返回某张表中不匹配的一些记录数 3).自连接:从始至终都是一张表,模拟一张表派生为两张(它们的结构式一模一样的),自己连自己 等值连接中的内连接: 需求: 查询所有员工的员工号、员工姓名以及部门的名字? select employee_id,last_name,department_name from employees,departments; 【注意】 以上查询得到了2889条记录,很多都是没有用的数据(脏数据), 出现的原因是:没有添加有效的连接条件导致的, 而这种现象我们称为笛卡尔集现象; 我们日后的学习和开发环境中是绝对要避免的!! 如何保证我们之后的多表查询绝对不会出现笛卡尔集现象? 1).不能不写连接条件 2).连接条件必须是有效的 思考:如何修改上述的代码? 代码实现如下: select employee_id,last_name,department_name from employees,departments where employees.department_id = departments.department_id; 需求:使用内连接来实现 查询员工的员工号、姓名、部门号、部门名字? select employee_id,last_name,department_id,department_name from employees,departments where employees.department_id = departments.department_id; 以上代码出错了,出错原因: 因为对于department_id这个列在employees和departments两张表中都存在, 所以需要显示的告诉编译器,我从哪张表中获取数据内容的! 修改代码如下: select employee_id,last_name,departments.department_id,department_name from employees,departments where employees.department_id = departments.department_id; select employee_id,last_name,employees.department_id,department_name from employees,departments where employees.department_id = departments.department_id; 思考:没有重复的列可以使用名字.的形式来定义吗?---> 可以的 select employee.employee_id,employee.last_name,employees.department_id,departments.department_name from employees,departments where employees.department_id = departments.department_id; 上述代码运行以及结果方面不存在问题,但是在代码量上比较冗余!!我们可以使用如下的方式解决... 给名字起别名的方式: 修改代码如下: select e.employee_id,e.last_name,e.department_id,d.department_name from employees e,departments d where e.department_id = d.department_id; 总结:对于多表查询,如果涉及n张表,至少需要有n-1个连接条件; 非等值连接: 需求: 查询员工的姓名、薪资以及薪资的等级 select last_name,salary,grade_level from employees,job_grades where salary between lowest_sal and highest_sal; 以上代码有问题,可以看到各个人的薪资等级,但是由于没有追加连接连接,还是出现了笛卡尔集现象; 我们需要慎用!一般之后非等值连接用的比较少,而且必须配合等值连接一起用;
学习python第四天——Oracle查询的更多相关文章
- 学习python第四天——Oracle分组
1.分组的概念: 关键字:group by子句 结论:在select列表中如果出现了聚合函数,不是聚合函数的列,必须都要定义到group by子句的后面 需求: 查询公司各个部门的平均工资? sele ...
- Solr学习总结(四)Solr查询参数
今天还是不会涉及到.Net和数据库操作,主要还是总结Solr 的查询参数,还是那句话,只有先明白了solr的基础内容和查询语法,后续学习solr 的C#和数据库操作,都是水到渠成的事.这里先列出sol ...
- oracle学习笔记(四)oracle内存优化
emca -config dbcontrol db -repos recreate 解决'oracle Environment variable ORACLE_SID not defined. Ple ...
- 【Python】Java程序员学习Python(四)— 内置方法和内置变量
<假如爱有天意> 当天边那颗星出现,你可知我又开始想念,有多少爱恋只能遥遥相望,就像月光洒向海面,年少的我们曾以为,相爱的人就能到永远,当我们相信情到深处在一起,听不见风中的叹息,谁知道爱 ...
- 学习python,第四篇:Python 3中bytes/string的区别
原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...
- 系统学习python第四天学习笔记
1.解释 / 编译补充 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. 解释型:写完代码交给解释器,解释器会从上到下一行行代码执行:边解释边执行. [实时翻译] 2.字符串功能 ...
- 学习Python第四天
关于剩下的数据类型:字符串 字符串是有序的,不可变的(不可变的意思是指将变量a重新赋值后不会覆盖原来的值,而是在内存中开辟了一块新的内存地址,存储变量的值) 字符串的各种方法: 1,将字符串中的大写变 ...
- 学习python第四天 列表
模块的导入是使用 import sys#导入模块sysprint(sys.path)#打印环境变量,可能存在的目录print(sys.argv)#打印脚本的名字,相对路径 import os os.s ...
- 【Python】Java程序员学习Python(五)— 函数的定义和使用
不想做一个待宰的羔羊!!!!要自己变得强大.... 函数的定义和使用放在最前边还是有原因的,现在语言趋于通用,基本类型基本都是那些,重点还是学习对象的使用方法,而最根本的还是方法的使用,因此优先介绍, ...
随机推荐
- Winform无边框窗体拖动
调用示例 当然,BUG还是有的,不过基本需求倒也可以
- Java并发常见问题
ConcurrentHashMap源码分析,参考:http://blog.csdn.net/do_smile/article/details/46911727 HashMap源码分析,参考:http: ...
- 无效的列类型:getTimestamp not implemented for class oracle.jdbc.driver.T4CNumberAccessor
错误信息: 无效的列类型:getTimestamp not implemented for class oracle.jdbc.driver.T4CNumberAccessor 错误原因:经过排查发现 ...
- css之背景(background)家族
背景(background)是css中很重要的一部分,也是css的基础知道之一,现在来回顾css2中5个属性与css3中新增的3个属性和2个功能. CSS2_背景(background)前传 家族成员 ...
- 多线程下载英文Google地图
1. pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- Android View的事件分发机制和滑动冲突解决方案
这篇文章会先讲Android中View的事件分发机制,然后再介绍Android滑动冲突的形成原因并给出解决方案.因水平有限,讲的不会太过深入,只希望各位看了之后对事件分发机制的流程有个大概的概念,并且 ...
- flutter圆角效果的实现
new Material( borderRadius: BorderRadius.circular(20.0), shadowColor: Colors.blue.shade200, elevatio ...
- Android sdcard之read-only
AndroidManifest.xml是否加入了SDCard的权限设置 <!-- 创建与删除文件权限 --> <uses-permission android:name=" ...
- vue使用百度地图
1.在百度地图申请密钥:http://lbsyun.baidu.com/ 将 <script type="text/javascript" src="http:// ...
- 4.Servlet过滤器
1.Servlet 编写过滤器 Servlet 过滤器是可用于 Servlet 编程的 Java 类,有以下目的: 在客户端的请求访问后端资源之前,拦截这些请求. 在服务器的响应发送回客户端之前,处理 ...