SQL
SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table)
คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง
หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1, Column2, Column3,... 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 การเลือกข้อมูลที่ระบุฟิวด์
SELECT CustomerID, Name, Email FROM customer
Output
CustomerID
|
Name
|
Email
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
C002
|
John Smith
|
john.smith@thaicreate.com
|
C003
|
Jame Born
|
jame.born@thaicreate.com
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
--------------------------------------------------------------------------------------------------------------------
SQL
WHERE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข
หรือมากกว่า 1 เงื่อนไข
Database : MySQL,Microsoft Access,SQL Server,Oracle
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'
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
|
---------------------------------------------------------------------------------------------------------------------
SQL ALIAS
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย ALIAS คือการสร้างชื่อจำลองขึ้นมาใหม่ โดยสามารถจำลองชื่อได้ทั้งชื่อ Field และชื่อ Table
Database : MySQL
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 |
--------------------------------------------------------------------------------------------------------------------
SQL OR AND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
Database : MySQL,Microsoft Access,SQL Server,Oracle
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'
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
SELECT Culumn1,Culumn2,Culumn3,... FROM [Table-Name] ORDER BY [Field] [ASC/DESC],[Field] [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
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
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
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,OracleSyntax
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
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 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
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
|
--------------------------------------------------------------------------------------------------------------------
SQL OUTER JOIN เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ OUTER JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกและ Table สอง ถ้าไม่มีข้อมูลใน Table แรก และ Table สองที่เชื่อมโยงกัน ข้อมูล Table แรกและ Table สอง จะไม่ถูกสนใจ Database : Microsoft Access,SQL Server,OracleSyntax
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 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
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql.html