วันอังคารที่ 9 ธันวาคม พ.ศ. 2557

SQL OUTER JOIN



SQL OUTER JOIN 


เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ OUTER JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกและ Table สอง ถ้าไม่มีข้อมูลใน Table แรก และ Table สองที่เชื่อมโยงกัน ข้อมูล Table แรกและ Table สอง จะไม่ถูกสนใจ 


Database : Microsoft Access,SQL Server,Oracle

Syntax

SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1],[Table-Name2]
WHERE [Table-Name1].Column (+)= [Table-Name2].Column


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C006
Superman Returnsupermain.return@thaicreate.com
US
20000000

Table : audit
AuditID
CustomerID
Date
Used
1
C001
2008-07-01
100000
2
C001
2008-07-05
200000
3
C001
2008-07-10
300000
4
C002
2008-07-02
400000
5
C002
2008-07-07
100000
6
C002
2008-07-15
300000
7
C003
2008-07-20
400000
8
C003
2008-07-25
200000
9
C004
2008-07-04
100000
10
C005
2008-07-04
200000


Sample1 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit 

SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID (+)= audit.CustomerID


Output 

CustomerID
Name
Email
CountryCode
Budget
Used
Date
Used
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000
2008-08-01
100000
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000
2008-08-05
200000
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000
2008-08-10
300000
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
2008-08-02
400000
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
2008-08-07
100000
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
2008-08-15
300000
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000
2008-08-20
400000
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000
2008-08-25
200000
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000
2008-07-04
100000
Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 
SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID (+)= audit.CustomerID
AND customer.CustomerID (+)= 'C001'

Output 
CustomerID
Name
Email
AuditID
CustomerID
Date
Used
C001
Win Weerachai
win.weerachai@thaicreate.com
1
C001
2008-08-01
100000
C001
Win Weerachai
win.weerachai@thaicreate.com
2
C001
2008-08-05
200000
C001
Win Weerachai
win.weerachai@thaicreate.com
3
C001
2008-08-10
300000

Sample3 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit 
SELECT audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'

Output 
AuditID
CustomerID
Date
Used
1
C001
2008-08-01
100000
2
C001
2008-08-05
200000
3
C001
2008-08-10
300000

ไม่มีความคิดเห็น:

แสดงความคิดเห็น