python针对Oracle常见查询操作实例分析


Posted in Python onApril 30, 2020

本文实例讲述了python针对Oracle常见查询操作。分享给大家供大家参考,具体如下:

1.子查询(难):

当进行查询的时候,发现需要的数据信息不明确,需要先通过另一个查询得到,

此查询称为子查询;

执行顺序:先执行子查询得到结果以后返回给主查询

组成部分:

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'

);

2.多表查询/多表联查

概念:

使用场景,如果一条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数据库示例:

import cx_Oracle
conn = cx_Oracle.connect('scott/tiger@localhost:1521/orcl')
cursor = conn.cursor()
cursor.execute("SELECT ENAME FROM EMP")
row = cursor.fetchone()
print row[0],

cursor.close()
conn.close()

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python中处理时间的几种方法小结
Apr 09 Python
Python tkinter模块弹出窗口及传值回到主窗口操作详解
Jul 28 Python
Python实现的人工神经网络算法示例【基于反向传播算法】
Nov 11 Python
python计算日期之间的放假日期
Jun 05 Python
Python OpenCV对本地视频文件进行分帧保存的实例
Jan 08 Python
python抖音表白程序源代码
Apr 07 Python
使用TensorFlow实现简单线性回归模型
Jul 19 Python
django将数组传递给前台模板的方法
Aug 06 Python
使用python脚本自动创建pip.ini配置文件代码实例
Sep 20 Python
PyTorch和Keras计算模型参数的例子
Jan 02 Python
python中的socket实现ftp客户端和服务器收发文件及md5加密文件
Apr 01 Python
Python使用海龟绘图实现贪吃蛇游戏
Jun 18 Python
python实现Oracle查询分组的方法示例
Apr 30 #Python
Pytorch数据拼接与拆分操作实现图解
Apr 30 #Python
如何安装并在pycharm使用selenium的方法
Apr 30 #Python
Python基于进程池实现多进程过程解析
Apr 30 #Python
使用Python合成图片的实现代码(图片添加个性化文本,图片上叠加其他图片)
Apr 30 #Python
解决Python发送Http请求时,中文乱码的问题
Apr 30 #Python
Pytorch高阶OP操作where,gather原理
Apr 30 #Python
You might like
利用Ffmpeg获得flv视频缩略图和视频时间的代码
2011/09/15 PHP
CentOS6.5 编译安装lnmp环境
2014/12/21 PHP
yii插入数据库防并发的简单代码
2017/05/27 PHP
JAVASCRIPT  THIS详解 面向对象
2009/03/25 Javascript
JavaScript 在各个浏览器中执行的耐性
2009/04/06 Javascript
ymPrompt的doHandler方法来实现获取子窗口返回值的方法
2010/06/25 Javascript
完美解决AJAX跨域问题
2013/11/01 Javascript
js实现简单计算器
2015/11/22 Javascript
String字符串截取的四种方式总结
2016/11/28 Javascript
angular仿支付宝密码框输入效果
2017/03/25 Javascript
JavaScript使用atan2来绘制箭头和曲线的实例
2017/09/14 Javascript
Node.js利用断言模块assert进行单元测试的方法
2017/09/28 Javascript
Vue完整项目构建(进阶篇)
2018/02/10 Javascript
react在安卓中输入框被手机键盘遮挡问题的解决方法
2018/09/03 Javascript
vue实现类似淘宝商品评价页面星级评价及上传多张图片功能
2018/10/29 Javascript
[07:48]DOTA2上海特级锦标赛主赛事首日RECAP
2016/03/04 DOTA
python通过线程实现定时器timer的方法
2015/03/16 Python
在Python中使用swapCase()方法转换大小写的教程
2015/05/20 Python
python读取.mat文件的数据及实例代码
2019/07/12 Python
Python将string转换到float的实例方法
2019/07/29 Python
解决Pycharm 包已经下载,但是运行代码提示找不到模块的问题
2019/08/31 Python
Pytorch之卷积层的使用详解
2019/12/31 Python
pytorch 获取tensor维度信息示例
2020/01/03 Python
150行python代码实现贪吃蛇游戏
2020/04/24 Python
python读取excel数据绘制简单曲线图的完整步骤记录
2020/10/30 Python
HTML5有哪些新特征
2015/12/01 HTML / CSS
HTML5触摸事件演化tap事件介绍
2016/03/25 HTML / CSS
卡西欧B级产品官方网站:Casio Outlet
2018/05/22 全球购物
纽约海:Sea New York
2018/11/04 全球购物
英国网上超市:Ocado
2020/03/05 全球购物
AJAX的优缺点都有什么
2015/08/18 面试题
口腔医学技术应届生求职信
2013/11/09 职场文书
比赛口号大全
2014/06/10 职场文书
最美家庭活动方案
2014/08/31 职场文书
消夏晚会主持词
2015/06/30 职场文书
Redis做数据持久化的解决方案及底层原理
2021/07/15 Redis