python查询oracle数据库_python针对Oracle常见查询操作实例分析

本文实例讲述了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编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

本文标题: python针对Oracle常见查询操作实例分析

本文地址: http://www.cppcns.com/shujuku/oracle/311365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种高级编程语言,可以用于完成许多自动化任务,包括查询数据库字段。下面是使用Python编写自动化查询数据库字段的工具的步骤和实现方法: 1.安装Python数据库连接驱动程序:Python是一种开放源代码语言,可以使用它的许多库来连接数据库,如MySQL,PostgreSQL和Oracle等。在这里,我们以MySQL数据库为例,使用PyMySQL库来连接MySQL数据库。 2.编写Python脚本:编写Python脚本来自动查询数据库字段需要使用Python的一些库。在这里,我们将使用PyMySQL库来连接MySQL数据库,并使用pandas库来读取数据和处理数据。在脚本中,我们需要提供用户名,密码,主机名,端口以及数据库名称等必要的参数。 3.连接MySQL数据库:我们需要提供数据库名,用户和密码等信息,以便成功连接到MySQL服务器。 4.读取数据库中的表:使用pandas库从MySQL数据库中读取数据。我们需要指定要检索的数据表,然后使用pandas库来读取数据并存储在一个数据帧中。 5.查询字段:针对每个数据表,我们可以通过指定要查询的字段来自动查询数据库字段。 6.输出结果:自动查询数据库字段后,我们可以使用pandas库将结果输出为Excel或CSV格式的文件。 通过以上步骤,我们就能够使用Python编写一个自动化查询数据库字段的工具。使用Python编写自动化工具可以大大提高工作效率,减少手动工作的时间和误差。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值