1.SQL LEFT JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table)
โดยเงื่อนไขการ LEFT JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน
โดยจะทำการอิงจาก Table แรกสำคัญก่อน ถ้าไม่มีข้อมูลใน Table
แรก ข้อมูล Table สองจะไม่ถูกสนใจและจะสนใจข้อมูลแค่
Table แรกเท่านั้น
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT [Table-Name1].Column1,
[Table-Name2].Column1,... FROM [Table-Name1]
LEFT JOIN [Table-Name2] ON [Table-Name1].Column = [Table-Name2].Column
LEFT JOIN [Table-Name2] ON [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 การเลือกข้อมูลแบบ LEFT JOIN ตาราง customer และ audit
SELECT customer.*,audit.* FROM customer
LEFT JOIN audit ON customer.CustomerID = audit.CustomerID
LEFT JOIN audit ON 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
|
|
C006
|
Superman Return
|
supermain.return@thaicreate.com
|
US
|
2000000
|
0
|
NULL
|
NULL
|
NULL
|
NULL
|
Sample2 การเลือกข้อมูลแบบ LEFT JOIN ตาราง customer และ audit และ CustomerID = C001
SELECT customer.*,audit.* FROM customer
LEFT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE customer.CustomerID = 'C001'
LEFT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE 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 การเลือกข้อมูลแบบ LEFT JOIN ตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit
SELECT audit.* FROM customer
LEFT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE customer.CustomerID = 'C001'
LEFT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE 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 OR AND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table)
การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
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'
หรือ
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
|
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table)
ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : Microsoft Access,SQL Server
Syntax
Database : Microsoft Access,SQL Server
Syntax
SELECT TOP [Integer] Column1,
Column2, Column3,... FROM [Table-Name] ORDER BY [Field] [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 การเลือกข้อมูลที่จำนวน Budget มากที่สุดออกมา 2 Record
SELECT TOP 2 * FROM customer
ORDER BY Budget DESC
Output
|
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
|
|
4.SQL WHERE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข หรือมากกว่า 1 เงื่อนไข
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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'
หรือ แบบ 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
|
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table)
โดยทำการปัดเศษขึ้นในกรณีที่มากกว่า >= .5 และปัดเศษลงในกรณีที่น้อยกว่า
< .5
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT ROUND(ColumnName) FROM table_name
6. SQL SUM
คำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าผลรวมของฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
คำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าผลรวมของฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT SUM(Column/Field) AS
[New-Field] 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 การเลือกข้อมูลผลรวมของ Budget
SELECT SUM(Budget) AS SumBudget FROM
customer
Output
|
SumBudget
|
|
10000000
|
7. SQL MID
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดคำใน Colomn หรือ Field ที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดคำใน Colomn หรือ Field ที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT MID(column_name,start[,length]) 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 เลือกข้อมูล Column ชือ่ Name ออกมา 4 ตัวอักษร
SELECT MID(Name,1,4) As Name FROM customer
Output
|
Name
|
|
Win
|
|
John
|
|
Jame
|
|
Chal
|
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table)
โดยเลือกเฉพาะค่าที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
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 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 = C002 และ C003
SELECT * FROM customer WHERE CustomerID IN ('C002','C003')
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
|
9. SQL LIKE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... FROM
[Table-Name] WHERE [Filed] LIKE '%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 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
SELECT * FROM customer WHERE Name LIKE '%ee%'
Output
|
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
Sample2 การเลือกข้อมูลตารางที่ฟิวด์ Email มีคำว่า j นำหน้า
SELECT * FROM customer WHERE Name LIKE 'j%'
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
|
Sample3 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า i ลงท้าย
SELECT * FROM customer WHERE Name LIKE '%i'
Output
|
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
การใช้งาน LIKE ที่หลายเงื่อนไข หรือ OR ใน Statement สามารถใช้ [x,y] ได้
เช่น
SELECT * FROM customer WHERE Name LIKE
'%[John,jame]%'
10. SQL WEEK()
เป็นคำสั่งของ MySQL กับ DateTime โดย WEEK ใช้ในการหาสัปดาห์ของปี
Database : MySQL
Syntax
เป็นคำสั่งของ MySQL กับ DateTime โดย WEEK ใช้ในการหาสัปดาห์ของปี
Database : MySQL
Syntax
WEEK(date[,mode])
The following table describes how the mode argument works.
|
Mode
|
First
day of week
|
Range
|
Week
1 is the first week …
|
|
0
|
Sunday
|
0-53
|
with a Sunday in this
year
|
|
1
|
Monday
|
0-53
|
with more than 3 days
this year
|
|
2
|
Sunday
|
1-53
|
with a Sunday in this
year
|
|
3
|
Monday
|
1-53
|
with more than 3 days
this year
|
|
4
|
Sunday
|
0-53
|
with more than 3 days
this year
|
|
5
|
Mnday
|
0-53
|
with a Monday in this
year
|
|
6
|
Sunday
|
1-53
|
with more than 3 days
this year
|
|
7
|
Monday
|
1-53
|
with a Monday in this
year
|
Sample
SELECT WEEK('2008-02-20');
-> 7
SELECT WEEK('2008-02-20',0);
-> 7
SELECT WEEK('2008-02-20',1);
-> 8
SELECT WEEK('2008-12-31',1);
-> 35
-> 7
SELECT WEEK('2008-02-20',0);
-> 7
SELECT WEEK('2008-02-20',1);
-> 8
SELECT WEEK('2008-12-31',1);
-> 35
ที่มา:http://www.thaicreate.com/tutorial/sql.html