วันอาทิตย์ที่ 28 ธันวาคม พ.ศ. 2557

ตัวอย่างคำสั่ง SQL


ตัวอย่างคำสั่ง SQL
ตัวอย่างที่1
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 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 Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
4
C002
2008-08-02
400000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
5
C002
2008-08-07
100000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
6
C002
2008-08-15
300000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
7
C003
2008-08-20
400000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
8
C003
2008-08-25
200000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
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'

Output

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
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




ตัวอย่างที่2
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 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

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 Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


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'


Output


CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000


CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000


CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000





ตัวอย่างที่3
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 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

Sample1 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อฟิวด์ขึ้นมาใหม่

SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer

Output


CusID
CusName
CusEmail
C001
Win Weerachaiwin.weerachai@thaicreate.com
C002
John Smithjohn.smith@thaicreate.com
C003
Jame Bornjame.smith@thaicreate.com
C004
Chalee Angelchalee.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'

Output

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
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



ตัวอย่างที่4
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 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
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 Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000

CustomerID
Name
Email
CountryCode
Budget
Used
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000



ตัวอย่างที่5
SQL IN 

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] IN ('Value1','Value2')

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
Sample1 การเลือกข้อมูลที่ CustomerID = C002 และ C003

SELECT * FROM customer WHERE CustomerID IN ('C002','C003')

Output

CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000





ตัวอย่างที่6
SQL COUNT

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวน Count Record ที่ค้นพบ

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT COUNT(Column/Field) AS [New-Field] FROM [Table-Name]

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

Sample1 การเลือกข้อมูลจำนวนลูกค้าทั้งหมด

SELECT COUNT(CustomerID) AS CountCustomerID FROM customer

Output

CountCustomerID
4





ตัวอย่างที่7
SQL GROUP BY 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้หาผลรวมของคอลัมน์จากแถวใน Column ที่ระบุและทำการรวม Group ภายใต้ Column ที่อยู่หลัง GROUP BY

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column,SUM(Column) FROM [Table-Name] GROUP BY 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

Sample1 การเลือกข้อมูลผลรวมของ Budget โดยแบ่ง Group ตาม CountryCode

SELECT CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode

Output

CountryCode
SumBudget
EN
2000000
TH
1000000
US
7000000





ตัวอย่างที่8
SQL CONCAT 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการรวมข้อความเข้าด้วยกัน

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT CONCAT(Culumn1,Culumn2,...) AS [New-Field] FROM [Table-Name]

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
Sample1 การเลือกข้อมูลโดยทำการรวมฟิวด์ CustomerID,Name,Email

SELECT CustomerID,CONCAT(CustomerID,Name,Email) AS ConcatField FROM customer

Output

CustomerID
ConcatField
C001
C001Win Weerachaiwin.weerachai@thaicreate.com
C002
C002John Smithjohn.smith@thaicreate.com
C003
C003Jame Bornjame.smith@thaicreate.com
C004
C004Chalee Angelchalee.angel@thaicreate.com




ตัวอย่างที่9
SQL REPLACE

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT REPLACE(Field,Search,Replace) AS [New-Field] FROM [Table-Name]

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

Sample1 การเลือกข้อมูลโดยทำการแทนที่ค่า Win ด้วย Mr.Win

SELECT REPLACE(Name, 'Win', 'Mr.Win') AS MyName FROM customer

Output

MyName
Mr.Win Weerachai
John Smith
Jame Born
Chalee Angel


ตัวอย่างที่10
SQL AVG 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าเฉลี่ยผลรวมของฟิวด์

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT AVG(Column/Field) AS [New-Field] FROM [Table-Name]

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

Sample1 การเลือกข้อมูลค่าเฉลี่ยผลรวมของ Budget

SELECT AVG(Budget) AS AvgBudget FROM customer

Output

AvgBudget
2500000