ตัวอย่างSQL 10รูปแบบ
SQL BETWEEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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] 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 OUTER JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ OUTER JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกและ Table สอง ถ้าไม่มีข้อมูลใน Table แรก และ Table สองที่เชื่อมโยงกัน ข้อมูล Table แรกและ Table สอง จะไม่ถูกสนใจ
Database : Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
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
|
SQL LEFT JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ LEFT JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกสำคัญก่อน ถ้าไม่มีข้อมูลใน Table แรก ข้อมูล Table สองจะไม่ถูกสนใจและจะสนใจข้อมูลแค่ Table แรกเท่านั้น
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ LEFT JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table แรกสำคัญก่อน ถ้าไม่มีข้อมูลใน Table แรก ข้อมูล Table สองจะไม่ถูกสนใจและจะสนใจข้อมูลแค่ Table แรกเท่านั้น
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
|
SQL DISTINCT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการเลือกข้อมูลที่ซ้ำกันมาเพียงแค่ Record เดียว
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการเลือกข้อมูลที่ซ้ำกันมาเพียงแค่ Record เดียว
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT DISTINCT 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 การเลือกข้อมูล CountryCode ที่ไม่ซ้ำกัน
SELECT DISTINCT CountryCode
FROM customer
Output
|
CountryCode
|
|
TH
|
|
EN
|
|
US
|
|
SQL LIMIT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : MySQL
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : MySQL
Syntax
SELECT Column1, Column2,
Column3,... FROM [Table-Name] ORDER BY [Fields] [ASC/DESC] LIMIT [Int-Start]
, [Int-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 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
SELECT * FROM customer
ORDER BY Used DESC LIMIT 0,2
Output
|
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|
C002
|
John
Smith
|
john.smith@thaicreate.com
|
EN
|
2000000
|
800000
|
|
C001
|
Win
Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
SQL RAND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ในรูปแบบของการสุ่ม Record
Database : MySQL
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ในรูปแบบของการสุ่ม Record
Database : MySQL
Syntax
SELECT Column1, Column2,
Column3,... FROM [Table-Name] ORDER BY RAND() LIMIT [Int]
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 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
SELECT * FROM customer ORDER
BY RAND() LIMIT 2
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
|
MYSQL Full-Text Search (ค้นหาข้อความแบบเต็ม)
อธิบายความเข้าใจเกี่ยวกับการนำ MySQL FullText จากการค้นหาระบบ ในแบบเดิมซึ่งจะใช้การค้นหาด้วย LIKE '%Keyword%' ซึ่งจะไปค้นหา จาก Keyword ที่มีคำนั้น ๆ อยู่ใน Column
หรือ ฟิวด์ใน Database เช่น
เช่น
เช่น
Welcome to
www.ThaiCreate.Com the best free tutorials
กรณีใช้ LIKE '%fr%' ก็จะได้ Record นี้มาเพราะคำว่า fr มีอยู่ในประโยค free
แต่ในระบบ Full Text ข้อความจะถูกจัดทำดัชนี หรือ Index แยกประโยคและความหมายของศัพท์คำ ประโยคหรือข้อความที่ไม่มีความสำคัญต่อการค้นหา ก็จะไม่ถูกจัดทำดัชนีเป็น Index และจะจัดทำดัชนีสำหรับประโยคที่สำคัญ ๆ ไว้สำหรับการค้นหา ซึ่งวิธีการนี้จะทำให้เราสามารถค้นหาข้อมูลได้ตรงกับความต้องการมากที่สุด ซึ่งจะแต่กต่างกับการใช้ LIKE จะได้ผลลัพธ์
[Welcome] [to] [www.ThaiCreate.Com]
[the] [best] [free] [tutorials]
จากการทำดัชนี (Index) แยกคำหรือประโยคได้แล้วก็จะถูกนำไปจัดเก็บกับความสำคัญของคำศัพท์ ไว้สำหรับเปรียบเทียบคำต่อคำ คำไหนที่ไม่มีประโยชน์ เช่น to , the .. ก็จะไม่ถูกทำดับชนีไว้สำหรับค้นหา กรณีใช้ค้นหา fr จะไม่พบ Record ดังกล่าว ซึ่งจะช่วยให้การค้นหานั้น สามารถค้นหาได้อย่างรวดเร็ว เพราะได้จัดทำ index ไว้แล้ว และยังตรงกับความต้องการมากที่สุด
การใช้งาน MySQL FULLTEXT
สำหรับ MySQL FULLTEXT นั้นรองรับ DataType อยู่ 3 ประเภทคือ CHAR VARCHAR แล้วก็ TEXT ซึ่งในการกำหนดคุณสมบัติของ Table และ Column ให้เป็นแบบ FULLTEXT สามารถกำหนดได้ในขั้นตอนการ CREATE TABLE
CREATE TABLE `customer`
(
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`),
FULLTEXT (`Name`,`Email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`),
FULLTEXT (`Name`,`Email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
MYSQL LAST_DAY()
เป็นคำสั่งของ MySQL กับ DateTime โดย LAST_DAY ใช้ในการหาวันที่สุดท้ายของเดือน
Database : MySQL
Syntax
เป็นคำสั่งของ MySQL กับ DateTime โดย LAST_DAY ใช้ในการหาวันที่สุดท้ายของเดือน
Database : MySQL
Syntax
LAST_DAY(date)
Sample
SELECT
LAST_DAY('2003-02-05');
-> 2003-02-28
SELECT LAST_DAY('2004-02-05');
-> 2004-02-29
SELECT LAST_DAY('2004-01-01 01:01:01');
-> 2004-01-31
SELECT LAST_DAY('2003-03-32');
-> NULL
-> 2003-02-28
SELECT LAST_DAY('2004-02-05');
-> 2004-02-29
SELECT LAST_DAY('2004-01-01 01:01:01');
-> 2004-01-31
SELECT LAST_DAY('2003-03-32');
-> NULL
MYSQL TIME_FORMAT()
เป็นคำสั่งของ MySQL กับ DateTime โดย TIME_FORMAT ใช้ในการหาหรือแปลงค่า ของเวลา ในรูปแบบต่าง ๆ
Database : MySQL
Syntax
เป็นคำสั่งของ MySQL กับ DateTime โดย TIME_FORMAT ใช้ในการหาหรือแปลงค่า ของเวลา ในรูปแบบต่าง ๆ
Database : MySQL
Syntax
TIME_FORMAT(time,format)
|
Specifier
|
Description
|
|
%a
|
Abbreviated
weekday name (Sun..Sat)
|
|
%b
|
Abbreviated
month name (Jan..Dec)
|
|
%c
|
Month,
numeric (0..12)
|
|
%D
|
Day
of the month with English suffix (0th, 1st, 2nd, 3rd, …)
|
|
%d
|
Day
of the month, numeric (00..31)
|
|
%e
|
Day
of the month, numeric (0..31)
|
|
%f
|
Microseconds
(000000..999999)
|
|
%H
|
Hour
(00..23)
|
|
%h
|
Hour
(01..12)
|
|
%I
|
Hour
(01..12)
|
|
%i
|
Minutes,
numeric (00..59)
|
|
%j
|
Day
of year (001..366)
|
|
%k
|
Hour
(0..23)
|
|
%l
|
Hour
(1..12)
|
|
%M
|
Month
name (January..December)
|
|
%m
|
Month,
numeric (00..12)
|
|
%p
|
AM
or PM
|
|
%r
|
Time,
12-hour (hh:mm:ss followed by AM or PM)
|
|
%S
|
Seconds
(00..59)
|
|
%s
|
Seconds
(00..59)
|
|
%T
|
Time,
24-hour (hh:mm:ss)
|
|
%U
|
Week
(00..53), where Sunday is the first day of the week
|
|
%u
|
Week
(00..53), where Monday is the first day of the week
|
|
%V
|
Week
(01..53), where Sunday is the first day of the week; used with %X
|
|
%v
|
Week
(01..53), where Monday is the first day of the week; used with %x
|
|
%W
|
Weekday
name (Sunday..Saturday)
|
|
%w
|
Day
of the week (0=Sunday..6=Saturday)
|
|
%X
|
Year
for the week where Sunday is the first day of the week, numeric, four digits;
used with %V
|
|
%x
|
Year
for the week, where Monday is the first day of the week, numeric, four
digits; used with %v
|
|
%Y
|
Year,
numeric, four digits
|
|
%y
|
Year,
numeric (two digits)
|
|
%%
|
A
literal “%” character
|
|
%x
|
x,
for any “x” not listed above
|
MYSQL ADDTIME()
เป็นคำสั่งของ MySQL กับ DateTime โดย ADDTIME ใช้ในการหา เพิ่มหรอลดค่าของเวลา
Database : MySQL
Syntax
ADDTIME(expr1,expr2)
Sample
SELECT
ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');
-> 2008-01-02 01:01:01.000001
SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');
-> 03:00:01.999997
-> 2008-01-02 01:01:01.000001
SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');
-> 03:00:01.999997
MYSQL DATE_ADD() , DATE_SUB()