SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (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
|
Sample การเลือกข้อมูลที่ระบุฟิวด์
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 ALIAS
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย ALIAS คือการสร้างชื่อจำลองขึ้นมาใหม่ โดยสามารถจำลองชื่อได้ทั้งชื่อ Field และชื่อ Table
Database : MySQL
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
|
Sample การเลือกข้อมูลตาราง 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
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
|
Sample การเลือกข้อมูลที่ 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 BETWEEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
[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
|
Sample การเลือกข้อมูลที่ 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 TOP
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : Microsoft Access,SQL Server
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
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
|
Sample การเลือกข้อมูลที่จำนวน 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
|
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
|
Sample การเลือกข้อมูล CountryCode ที่ไม่ซ้ำกัน
SELECT DISTINCT CountryCode FROM customer
Output
CountryCode
|
TH
|
EN
|
US
|
SQL ROWNUM
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : Oracle
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name]
WHERE ROWNUM <= [Int-Limit]
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
|
Sample การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
SELECT * FROM customer WHERE ROWNUM <= 2 ORDER BY
Used DESC
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 NOT LIKE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด และไม่แสดง Record ที่ค้นพบ ซึ่งทำหน้าที่ตรงข้ามกับ LIKE
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด และไม่แสดง Record ที่ค้นพบ ซึ่งทำหน้าที่ตรงข้ามกับ LIKE
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE
[Filed] NOT 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
|
Sample การเลือกข้อมูลตารางที่ฟิวด์ Name มีไม่มีคำว่า ee อยู่
SELECT * FROM customer WHERE Name NOT LIKE '%ee%'
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 INSERT
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง (Table) โดยสามารถเพิ่มได้ทั้งแถวหรือว่าเพิ่มในส่วนของแต่ละฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง (Table) โดยสามารถเพิ่มได้ทั้งแถวหรือว่าเพิ่มในส่วนของแต่ละฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
INSERT INTO [Table-Name] (Column1,Column2,Column3,...)
VALUES ('Value1','Value2','Value3',...)
Table : country
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United states
|
Sample การเพิ่มข้อมูลลงใน Table
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
Output
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United states
|
CH
|
Chaina
|
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
|
Sample การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 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
|
ที่มา : http://www.thaicreate.com/tutorial/sql.html