SQL Database & Table
เป็นภาษาที่ใช้ในการติดต่อกับฐานข้อมูลหรือพูดอีกอย่างก็คือ เป็นภาษาที่ใช้ในการสั่งให้ฐานฐานข้อมูลกระทำการใด ๆ ตามคำสั่งที่เราสั่ง ซึ่งในการติดต่อฐานข้อมูลนั้น ไม่ว่าจะเป็น SQL Server , Microsoft Access , MySQL ,DB2 หรือแม้แต่ Oracle ก็จะต้องใช้คำสั่งภาษา SQL ในการควบคุมทั้งสิ้น และเราจะมาเรียนรู้ถึงคำสั่งพื้นฐาน ของ SQL ที่จำเป็นกัน
สำหรับตัวอย่างในบทเรียนนี้ ผมได้สร้าง Table ขึ้นมา 3 Table ครับ
1.customer เป็นตารางเก็บข้อมูลลูกค้า
2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า
3.country เป็นตารางเก็บข้อมูลประเทศ
1.customer เป็นตารางเก็บข้อมูลลูกค้า
2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า
3.country เป็นตารางเก็บข้อมูลประเทศ
Table : Customer
Table : audit
Table : country
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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 |
Table : country
CountryCode
|
CountryName
|
---|---|
TH
| Thailand |
EN
| English |
US
| United states |
SQL WHERE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข หรือมากกว่า 1 เงื่อนไข
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลโดยใช้ Operators = (เท่ากับ)
Output
Sample2 การเลือกข้อมูลโดยใช้ Operators != (ไม่เท่ากับ)
Output
Syntax
SELECT Column1, Column2, Column3,... FROM Table-Name WHERE [Field] = 'Value'
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลโดยใช้ Operators = (เท่ากับ)
SELECT * FROM customer WHERE CountryCode = 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode = 'US' and Budget = '4000000'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode = 'US' and Budget = '4000000'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample2 การเลือกข้อมูลโดยใช้ Operators != (ไม่เท่ากับ)
SELECT * FROM customer WHERE CountryCode != 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode != 'US' and CountryCode != 'EN'
หรือจะใช้ or
SELECT * FROM customer WHERE CountryCode != 'US' or Budget = '1000000'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode != 'US' and CountryCode != 'EN'
หรือจะใช้ or
SELECT * FROM customer WHERE CountryCode != 'US' or Budget = '1000000'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
SQL ALIAS
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย ALIAS คือการสร้างชื่อจำลองขึ้นมาใหม่ โดยสามารถจำลองชื่อได้ทั้งชื่อ Field และชื่อ Table
Database : MySQL
Syntax
Table : customer
Sample1 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อฟิวด์ขึ้นมาใหม่
Output
Sample2 การเลือกข้อมูลตาราง customer,audit โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
Output
Sample3 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
Output
Syntax
SELECT Column1 AS Alias1,Column2 AS Alias2,Column3 AS Alias3,... FROM [Table-Name1] Table Alias
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อฟิวด์ขึ้นมาใหม่
SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer
Output
CusID
|
CusName
|
CusEmail
|
---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
C002
| John Smith | john.smith@thaicreate.com |
C003
| Jame Born | jame.smith@thaicreate.com |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
Sample2 การเลือกข้อมูลตาราง customer,audit โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
SELECT X.*,Y.* FROM customer X
LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID
WHERE X.CustomerID = 'C001'
LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID
WHERE X.CustomerID = 'C001'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
3
|
C001
|
2008-08-10
| 300000 |
Sample3 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
SELECT X.CustomerID,X.Name FROM customer X
Output
CusID
|
CusName
|
---|---|
C001
| Win Weerachai |
C002
| John Smith |
C003
| Jame Born |
C004
| Chalee Angel |
SQL OR AND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000
Output
Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] = 'Value' [AND/OR] [Field] = 'Value'
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000
SELECT * FROM customer WHERE CountryCode = 'US' AND Used = '100000'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
SQL ORDER BY
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยจัดเรียงข้อมูลตามต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย
Output
Syntax
SELECT Culumn1,Culumn2,Culumn3,... FROM [Table-Name] ORDER BY [Field] [ASC/DESC],[Field] [ASC/DESC],...
ASC = น้อยไปหามาก
DESC = มากไปหาน้อย
ASC = น้อยไปหามาก
DESC = มากไปหาน้อย
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย
SELECT * FROM customer ORDER BY CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
SQL SUB SELECT QUERY
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้เลือกข้อมูลย่อยภายใน SELECT ย่อยอีกชั้นหนึ่งครับSUB SELECT QUERY เข้ามาช่วยในด้านความสะดวกและง่ายกว่าการ JOIN TABLE แต่ข้อเสียของ SUB SELECT คือ สามารถทำงานได้ช้ากว่า JOIN TABLE
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Table : audit
Sample1 การเลือกข้อมูลตาราง customer ที่เชื่อมโยงกับตาราง audit ที่มีการใช้ยอดเงินในแต่ล่ะครั้งมากกว่า 400000
Output
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] IN (SELECT ..... FROM ....)
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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 |
Sample1 การเลือกข้อมูลตาราง customer ที่เชื่อมโยงกับตาราง audit ที่มีการใช้ยอดเงินในแต่ล่ะครั้งมากกว่า 400000
SELECT * FROM customer WHERE CustomerID IN (SELECT CustomerID FROM audit WHERE Used >= '400000')
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
SQL SELECT INTO
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้การเลือกข้อมูลจากต้นทางไปยังปลายทาง นิยมใช้สำหรับการ Copy Table หรือทำการ Backup Table
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลตาราง customer เพื่อไปสำรองไว้ที่ customer_backup
Output
Table : customer_backup
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... INTO [New-Table] FROM [Table-Name]
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลตาราง customer เพื่อไปสำรองไว้ที่ customer_backup
SELECT * INTO customer_backup FROM customer
Output
Table : customer_backup
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
SQL BETWEEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการเลือกเงื่อนไขที่อยู่ระหว่างค่าเริ่มต้นและค่าสิ้นสุด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่ Budget ที่มีค่าตั้งแต่ 1000000 - 3000000
Output
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] BETWEEN [Value-Start] AND [Value-End]
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลที่ Budget ที่มีค่าตั้งแต่ 1000000 - 3000000
SELECT * FROM customer WHERE Budget BETWEEN '1000000' AND '3000000'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
SQL JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ JOIN จะกระทำเมื่อมีข้อมูลตั้งแต่ 2 Table ขึ้นไป โดยข้อมูลเหล่านั้นเป็นข้อมูลที่มีความสัมพันธ์และเชื่อมโยงกับข้อมูลหลัก
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Table : audit
Sample1 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit
Output
Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001
Output
Sample3 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit
Output
Database : MySQL,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
WHERE [Table-Name1].Column = [Table-Name2].Column
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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 |
Sample1 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit
SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
WHERE customer.CustomerID = audit.CustomerID
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
3
|
C001
|
2008-08-10
| 300000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
4
|
C002
|
2008-08-02
| 400000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
5
|
C002
|
2008-08-07
| 100000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
6
|
C002
|
2008-08-15
| 300000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
7
|
C003
|
2008-08-20
| 400000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
8
|
C003
|
2008-08-25
| 200000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
9
|
C004
|
2008-07-04
| 100000 |
Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001
SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
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'
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 |
SQL OUTER JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ OUTER JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกและ Table สอง ถ้าไม่มีข้อมูลใน Table แรก และ Table สองที่เชื่อมโยงกัน ข้อมูล Table แรกและ Table สอง จะไม่ถูกสนใจ
Database : Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Table : audit
Sample1 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit
Output
Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001
Output
Sample3 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit
Output
Syntax
SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1],[Table-Name2]
WHERE [Table-Name1].Column (+)= [Table-Name2].Column
WHERE [Table-Name1].Column (+)= [Table-Name2].Column
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C006
| Superman Return | supermain.return@thaicreate.com |
US
| 2000000 | 0 |
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
WHERE customer.CustomerID (+)= audit.CustomerID
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
3
|
C001
|
2008-08-10
| 300000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
4
|
C002
|
2008-08-02
| 400000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
5
|
C002
|
2008-08-07
| 100000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
6
|
C002
|
2008-08-15
| 300000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
7
|
C003
|
2008-08-20
| 400000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
8
|
C003
|
2008-08-25
| 200000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
9
|
C004
|
2008-07-04
| 100000 |
Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001
SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID (+)= audit.CustomerID
AND customer.CustomerID (+)= 'C001'
WHERE customer.CustomerID (+)= audit.CustomerID
AND customer.CustomerID (+)= 'C001'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
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'
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 |