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,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,salary from employees where salary < ( select min(salary) from employees where job_id = 'IT_PROG' );
2.多表查詢/多表聯查
概念:
使用場景,如果一條select語句中需要查詢的列遍佈多張資料表,
那麼我們就必須使用多表查詢了!!
分類:
等值連線和非等值連線
對於等值連線分方向:
1).內連線:返回多張表中共同滿足的資料,取交集
2).外連線(左、右、滿):返回內連線資料的同時還會繼續返回某張表中不匹配的一些記錄數
3).自連線:從始至終都是一張表,模擬一張表派生為兩張(它們的結構式一模一樣的),自己連自己
等值連線中的內連線:
需求:
查詢所有員工的員工號、員工姓名以及部門的名字?
select employee_id,department_name from employees,departments;
【注意】
以上查詢得到了2889條記錄,很多都是沒有用的資料(髒資料),
出現的原因是:沒有新增有效的連線條件導致的,
而這種現象我們稱為笛卡爾集現象;
我們日後的學習和開發環境中是絕對要避免的!!
如何保證我們之後的多表查詢絕對不會出現笛卡爾集現象?
1).不能不寫連線條件
2).連線條件必須是有效的
思考:如何修改上述的程式碼?
程式碼實現如下:
select employee_id,departments where employees.department_id = departments.department_id;
需求:使用內連線來實現
查詢員工的員工號、姓名、部門號、部門名字?
select employee_id,department_id,departments where employees.department_id = departments.department_id;
以上程式碼出錯了,出錯原因:
因為對於department_id這個列在employees和departments兩張表中都存在,
所以需要顯示的告訴編譯器,我從哪張表中獲取資料內容的!
修改程式碼如下:
select employee_id,departments.department_id,departments where employees.department_id = departments.department_id; select employee_id,employees.department_id,departments where employees.department_id = departments.department_id;
思考:沒有重複的列可以使用名字.的形式來定義嗎?---> 可以的
select employee.employee_id,employee.last_name,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程式設計有所幫助。