select查詢之四:連線查詢
其中外連線又分全外連線、左外連線與右外連線。
1、左外連線:
查詢所有員工的資訊並且列出員工的部門名稱
SQL> select e.last_name,d.department_id,d.department_name
2 from employees e
3 left outer join departments d
4 on e.department_id = d.department_id;
LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
------------------------- ------------- ------------------------------
Urman 100 Finance
Sciarra 100 Finance
Chen 100 Finance
Faviet 100 Finance
Greenberg 100 Finance
Gietz 110 Accounting
Higgins 110 Accounting
Grant
107 rows selected.
2、右外連線:
在HR使用者,查詢沒有任何職員的部門
SQL> select e.employee_id,d.department_name,d.department_id
2 from employees e
3 right outer join departments d
4 on e.department_id = d.department_id;
EMPLOYEE_ID DEPARTMENT_NAME DEPARTMENT_ID
----------- ------------------------------ -------------
Benefits 160
Manufacturing 170
Construction 180
Contracting 190
Operations 200
IT Support 210
NOC 220
IT Helpdesk 230
Government Sales 240
Retail Sales 250
Recruiting 260
......
3、全外連結:
SQL> select e.last_name,d.department_id,d.department_name
2 from employees e
3 full outer join departments d
4 on e.department_id = d.department_id;
LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
------------------------- ------------- ------------------------------
Ozer 80 Sales
Bloom 80 Sales
Fox 80 Sales
Smith 80 Sales
Bates 80 Sales
Kumar 80 Sales
Abel 80 Sales
Hutton 80 Sales
Taylor 80 Sales
Livingston 80 Sales
Grant
LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
------------------------- ------------- ------------------------------
210 IT Support
160 Benefits
150 Shareholder Services
250 Retail Sales
140 Control And Credit
260 Recruiting
200 Operations
120 Treasury
270 Payroll
130 Corporate Tax
180 Construction
123 rows selected.
4、With字句(右外連線):
SQL> with
2 t1 as
3 (select 1 as id,'a' as name from dual
4 union all
5 select 2,'b' from dual
6 union all
7 select 3,'c' from dual),
8 t2 as
9 (select 2 as id,'bb' as name from dual
10 union all
11 select 3,'cc' from dual
12 union all
13 select 4,'dd' from dual)
14 select t1.id,t1.name,t2.id,t2.name
15 from t1 right outer join t2
16 on t1.id = t2.id;
ID N ID NA
---------- - ---------- --
2 b 2 bb
3 c 3 cc
4 dd
5、自連線:查詢每個領導都管理哪些員工
LAST_NAME LAST_NAME MANAGER_ID
------------------------- ------------------------- ----------
King Cambrault 100
King De Haan 100
King Errazuriz 100
King Fripp 100
King Hartstein 100
King Kaufling 100
King Kochhar 100
King Mourgos 100
King Partners 100
King Raphaely 100
King Russell 100
LAST_NAME LAST_NAME MANAGER_ID
------------------------- ------------------------- ----------
King Vollman 100
King Weiss 100
King Zlotkey 100
Kochhar Baer 101
Kochhar Greenberg 101
Kochhar Higgins 101
Kochhar Mavris 101
Kochhar Whalen 101
.......
Hartstein Fay 201
Higgins Gietz 205
106 rows selected.
6、不等連結:
在SCOTT使用者,查詢EMP,SALGRADE這兩張表,顯示每個人的級別。顯示欄位:人員id,姓名,級別。
SQL> select e.empno,e.ename,g.grade
2 from emp e
3 join salgrade g
4 on e.sal between g.losal and g.hisal;
EMPNO ENAME GRADE
---------- ---------- ----------
7369 SMITH 1
7900 JAMES 1
7876 ADAMS 1
7521 WARD 2
7654 MARTIN 2
7934 MILLER 2
7844 TURNER 3
7499 ALLEN 3
14 rows selected.
7、複雜內連線:
SQL> select empno,ename,dname
2 from emp e,dept d
3 where e.deptno=d.deptno
4 and d.deptno=30;
EMPNO ENAME DNAME
---------- ---------- --------------
7499 ALLEN SALES
7521 WARD SALES
7654 MARTIN SALES
7698 BLAKE SALES
7844 TURNER SALES
7900 JAMES SALES
6 rows selected.
8、交叉連線:
2 from students,courses;
STUDENT_ID NAME COURSE_ID COURSE_NAME
---------- --------------- ---------- ------------------------------
45211 Mar 1002 Earth and Land
45212 Maro 1002 Earth and Land
45221 July 1002 Earth and Land
45222 Marry 1002 Earth and Land
45223 Mare 1002 Earth and Land
45214 Jone 1002 Earth and Land
45215 Kaha 1002 Earth and Land
45216 Kaka 1002 Earth and Land
45211 Mar 1003 Earth Song
45212 Maro 1003 Earth Song
45221 July 1003 Earth Song
STUDENT_ID NAME COURSE_ID COURSE_NAME
---------- --------------- ---------- ------------------------------
45216 Kaka 1102 With the Earth
56 rows selected.
左外連線,缺右表資訊,右外連線,缺左表資訊,
全連線就是將左右外連線聯合起來。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31392094/viewspace-2125909/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL講義第27講——select 查詢之自連線查詢MySql
- MySQL之連線查詢和子查詢MySql
- select查詢中union連線符
- MySQL之連線查詢MySql
- 連線查詢
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- 資料庫 - 連線查詢、巢狀查詢、集合查詢資料庫巢狀
- mysql查詢語句5:連線查詢MySql
- SQL連線查詢SQL
- oracle 連線查詢Oracle
- 【Oracle】--連線查詢Oracle
- select查詢之三:子查詢
- select查詢之一:普通查詢
- sql 連線查詢例項(left join)三表連線查詢SQL
- 資料庫學習(四)連線查詢資料庫
- 資料庫資料的查詢----連線查詢資料庫
- 水煮oracle31----連線查詢&合併查詢Oracle
- select查詢之五:分析函式在查詢的運用函式
- 【閃回特性之閃回查詢】使用閃回查詢(select as of)
- Oracle查詢轉換(四)連線謂詞推入Oracle
- select子查詢
- 查詢之折半查詢
- MySQL講義第 47 講——select 查詢之查詢練習(五)MySql
- oracle連線查詢詳解Oracle
- MySQL學習-連線查詢MySql
- Hibernate連線查詢join
- 【janes】多表查詢 外連線
- MySQL8:連線查詢MySql
- 離線查詢與線上查詢
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- MySql中的資料查詢語言(DQL)三:連線查詢MySql
- 【SQL查詢】集合查詢之INTERSECTSQL
- MySQL內連線查詢語句MySql
- 分組查詢連線號段
- Sql Server系列:多表連線查詢SQLServer
- SQL複雜查詢多表連線SQL
- select查詢之六:別名與拼接
- pgsql查詢優化之模糊查詢SQL優化