python針對Oracle常見查詢操作實例分析
本文實例講述了python針對Oracle常見查詢操作。分享給大家供大家參考,具體如下:
1.子查詢(難):
當進行查詢的時候,發現需要的數據信息不明確,需要先通過另一個查詢得到,
此查詢稱為子查詢;
執行順序:先執行子查詢得到結果以后返回給主查詢
組成部分:
1).主查詢部分
2).子查詢部分
【注意事項】:
子查詢一定需要被定義/包裹在小括號內部,可以認為是顯示的提升了代碼執行的優先級
需求1:
查詢薪資比Abel的高的有誰?
分析:
①.先查詢出Abel的薪資是多少?
②.將過濾條件定義為>①,然后進行查詢得到最終需要的結果
代碼實現:
select last_name,salaryfrom employeeswhere salary > (select salary from employeeswhere last_name = ’Abel’);
需求2:
查詢job_id與141號員工相同,salary比143號員工多的員工的姓名,job_id和salary?
代碼實現:
select last_name,job_id,salaryfrom employeeswhere job_id = (select job_idfrom employeeswhere employee_id = 141)and salary > (select salaryfrom employeeswhere employee_id = 143);
課堂練習:
1).返回公司工資最少的員工的employee_id,job_id和salary
select employee_id,job_id,salaryfrom employeeswhere salary = (select min(salary)from employees);
2).查詢平均工資高于公司平均工資的部門有哪些
select department_id,avg(salary)from employeesgroup by department_idhaving avg(salary) > (select avg(salary)from employees)order by department_id desc;
3).查詢最低工資大于20號部門最低工資的部門id和最低工資
select department_id,min(salary)from employeeswhere department_id is not nullgroup by department_idhaving min(salary) > (select min(salary)from employeeshaving department_id = 20);
4).返回其它職位中比job_id為’IT_PROG’中最低工資低的員工的員工號,姓名,job_id以及salary
select employee_id,last_name,job_id,salaryfrom employeeswhere salary < (select min(salary)from employeeswhere job_id = ’IT_PROG’);
2.多表查詢/多表聯查
概念:
使用場景,如果一條select語句中需要查詢的列遍布多張數據表,
那么我們就必須使用多表查詢了??!
分類:
等值連接和非等值連接
對于等值連接分方向:
1).內連接:返回多張表中共同滿足的數據,取交集
2).外連接(左、右、滿):返回內連接數據的同時還會繼續返回某張表中不匹配的一些記錄數
3).自連接:從始至終都是一張表,模擬一張表派生為兩張(它們的結構式一模一樣的),自己連自己
等值連接中的內連接:
需求:
查詢所有員工的員工號、員工姓名以及部門的名字?
select employee_id,last_name,department_namefrom employees,departments;
【注意】
以上查詢得到了2889條記錄,很多都是沒有用的數據(臟數據),
出現的原因是:沒有添加有效的連接條件導致的,
而這種現象我們稱為笛卡爾集現象;
我們日后的學習和開發環境中是絕對要避免的??!
如何保證我們之后的多表查詢絕對不會出現笛卡爾集現象?
1).不能不寫連接條件
2).連接條件必須是有效的
思考:如何修改上述的代碼?
代碼實現如下:
select employee_id,last_name,department_namefrom employees,departmentswhere employees.department_id = departments.department_id;
需求:使用內連接來實現
查詢員工的員工號、姓名、部門號、部門名字?
select employee_id,last_name,department_id,department_namefrom employees,departmentswhere employees.department_id = departments.department_id;
以上代碼出錯了,出錯原因:
因為對于department_id這個列在employees和departments兩張表中都存在,
所以需要顯示的告訴編譯器,我從哪張表中獲取數據內容的!
修改代碼如下:
select employee_id,last_name,departments.department_id,department_namefrom employees,departmentswhere employees.department_id = departments.department_id;select employee_id,last_name,employees.department_id,department_namefrom employees,departmentswhere employees.department_id = departments.department_id;
思考:沒有重復的列可以使用名字.的形式來定義嗎?---> 可以的
select employee.employee_id,employee.last_name,employees.department_id,departments.department_namefrom employees,departmentswhere employees.department_id = departments.department_id;
上述代碼運行以及結果方面不存在問題,但是在代碼量上比較冗余??!我們可以使用如下的方式解決...
給名字起別名的方式:
修改代碼如下:
select e.employee_id,e.last_name,e.department_id,d.department_namefrom employees e,departments dwhere e.department_id = d.department_id;
總結:對于多表查詢,如果涉及n張表,至少需要有n-1個連接條件;
非等值連接:
需求:
查詢員工的姓名、薪資以及薪資的等級
select last_name,salary,grade_levelfrom employees,job_gradeswhere salary between lowest_sal and highest_sal;
以上代碼有問題,可以看到各個人的薪資等級,但是由于沒有追加連接連接,還是出現了笛卡爾集現象;
我們需要慎用!一般之后非等值連接用的比較少,而且必須配合等值連接一起用;
附:Python連接與查詢oracle數據庫示例:
import cx_Oracleconn = 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程序設計有所幫助。
相關文章:
