python查询oracle数据库_python针对Oracle常见查询操作实例分析
本文實(shí)例講述了python針對(duì)Oracle常見查詢操作。分享給大家供大家參考,具體如下:
1.子查詢(難):
當(dāng)進(jìn)行查詢的時(shí)候,發(fā)現(xiàn)需要的數(shù)據(jù)信息不明確,需要先通過另一個(gè)查詢得到,
此查詢稱為子查詢;
執(zhí)行順序:先執(zhí)行子查詢得到結(jié)果以后返回給主查詢
組成部分:
1).主查詢部分
2).子查詢部分
【注意事項(xiàng)】:
子查詢一定需要被定義/包裹在小括號(hào)內(nèi)部,可以認(rèn)為是顯示的提升了代碼執(zhí)行的優(yōu)先級(jí)
需求1:
查詢薪資比Abel的高的有誰?
分析:
①.先查詢出Abel的薪資是多少?
②.將過濾條件定義為>①,然后進(jìn)行查詢得到最終需要的結(jié)果
代碼實(shí)現(xiàn):
select last_name,salary
from employees
where salary > (
select salary from employees
where last_name = 'Abel'
);
需求2:
查詢job_id與141號(hào)員工相同,salary比143號(hào)員工多的員工的姓名,job_id和salary?
代碼實(shí)現(xiàn):
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
);
課堂練習(xí):
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號(hào)部門最低工資的部門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'中最低工資低的員工的員工號(hào),姓名,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.多表查詢/多表聯(lián)查
概念:
使用場(chǎng)景,如果一條select語句中需要查詢的列遍布多張數(shù)據(jù)表,
那么我們就必須使用多表查詢了!!
分類:
等值連接和非等值連接
對(duì)于等值連接分方向:
1).內(nèi)連接:返回多張表中共同滿足的數(shù)據(jù),取交集
2).外連接(左、右、滿):返回內(nèi)連接數(shù)據(jù)的同時(shí)還會(huì)繼續(xù)返回某張表中不匹配的一些記錄數(shù)
3).自連接:從始至終都是一張表,模擬一張表派生為兩張(它們的結(jié)構(gòu)式一模一樣的),自己連自己
等值連接中的內(nèi)連接:
需求:
查詢所有員工的員工號(hào)、員工姓名以及部門的名字?
select employee_id,last_name,department_name
from employees,departments;
【注意】
以上查詢得到了2889條記錄,很多都是沒有用的數(shù)據(jù)(臟數(shù)據(jù)),
出現(xiàn)的原因是:沒有添加有效的連接條件導(dǎo)致的,
而這種現(xiàn)象我們稱為笛卡爾集現(xiàn)象;
我們?nèi)蘸蟮膶W(xué)習(xí)和開發(fā)環(huán)境中是絕對(duì)要避免的!!
如何保證我們之后的多表查詢絕對(duì)不會(huì)出現(xiàn)笛卡爾集現(xiàn)象?
1).不能不寫連接條件
2).連接條件必須是有效的
思考:如何修改上述的代碼?
代碼實(shí)現(xiàn)如下:
select employee_id,last_name,department_name
from employees,departments
where employees.department_id = departments.department_id;
需求:使用內(nèi)連接來實(shí)現(xiàn)
查詢員工的員工號(hào)、姓名、部門號(hào)、部門名字?
select employee_id,last_name,department_id,department_name
from employees,departments
where employees.department_id = departments.department_id;
以上代碼出錯(cuò)了,出錯(cuò)原因:
因?yàn)閷?duì)于department_id這個(gè)列在employees和departments兩張表中都存在,
所以需要顯示的告訴編譯器,我從哪張表中獲取數(shù)據(jù)內(nèi)容的!
修改代碼如下:
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;
思考:沒有重復(fù)的列可以使用名字.的形式來定義嗎?---> 可以的
select employee.employee_id,employee.last_name,employees.department_id,departments.department_name
from employees,departments
where employees.department_id = departments.department_id;
上述代碼運(yùn)行以及結(jié)果方面不存在問題,但是在代碼量上比較冗余!!我們可以使用如下的方式解決...
給名字起別名的方式:
修改代碼如下:
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;
總結(jié):對(duì)于多表查詢,如果涉及n張表,至少需要有n-1個(gè)連接條件;
非等值連接:
需求:
查詢員工的姓名、薪資以及薪資的等級(jí)
select last_name,salary,grade_level
from employees,job_grades
where salary between lowest_sal and highest_sal;
以上代碼有問題,可以看到各個(gè)人的薪資等級(jí),但是由于沒有追加連接連接,還是出現(xiàn)了笛卡爾集現(xiàn)象;
我們需要慎用!一般之后非等值連接用的比較少,而且必須配合等值連接一起用;
附:Python連接與查詢oracle數(shù)據(jù)庫示例:
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()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
本文標(biāo)題: python針對(duì)Oracle常見查詢操作實(shí)例分析
本文地址: http://www.cppcns.com/shujuku/oracle/311365.html
總結(jié)
以上是生活随笔為你收集整理的python查询oracle数据库_python针对Oracle常见查询操作实例分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 临夏治疗多囊卵巢综合症最好的医院推荐
- 下一篇: python游戏循环设置_Pygame: