คำสั่ง SQL
1. SQL LIKE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
|
2. SQL JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ JOIN จะกระทำเมื่อมีข้อมูลตั้งแต่ 2 Table ขึ้นไป โดยข้อมูลเหล่านั้นเป็นข้อมูลที่มีความสัมพันธ์และเชื่อมโยงกับข้อมูลหลัก
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
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
|
Sample การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001
SELECT customer.*,audit.* FROM
customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'
WHERE customer.CustomerID = audit.CustomerID
AND 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
|
3. SQL TOP
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
|
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 MAX
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าสูงสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าสูงสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT MAX(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 MAX(Budget) AS
MaxBudget FROM customer
Output
MaxBudget
|
4000000
|
5. SQL MIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าต่ำสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าต่ำสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT MIN(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 MIN(Budget) AS
MinBudget FROM customer
Output
MinBudget
|
1000000
|
6. SQL COUNT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวน Count Record ที่ค้นพบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
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 COUNT(CustomerID)
AS CountCustomerID FROM customer
Output
CountCustomerID
|
4
|
7. SQL UPDATE
เป็นคำสั่งที่ใช้สำหรับลบข้อมูลในตาราง (Table) โดยสามารถทำการลบได้หลาย Record ภายในคำสั่งเดียว หรือว่า Record เดียว ทั้งนี้ขึ้นอยู่กับ Where ที่ผูใช้เขียนขึ้นด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับลบข้อมูลในตาราง (Table) โดยสามารถทำการลบได้หลาย Record ภายในคำสั่งเดียว หรือว่า Record เดียว ทั้งนี้ขึ้นอยู่กับ Where ที่ผูใช้เขียนขึ้นด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
DELETE FROM [Table-Name] WHERE clause
Table
: country
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United
states
|
JP
|
Japan
|
Sample1 การลบข้อมูลลงใน Table
DELETE FROM country WHERE CountryCode = 'JP'
Output
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United
states
|
8. SQL FIRST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT FIRST(ColumnName) FROM
TableName
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 การเลือกข้อมูลแบบด้วย FIRST ในตาราง customer
SELECT FIRST(Name) As Name FROM
customer
Output
Name
|
Weerachai Nukitram
|
9. SQL LAST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวสุดท้ายของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวสุดท้ายของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT LAST(ColumnName) FROM
TableName
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 การเลือกข้อมูลแบบด้วย LAST ในตาราง customer
SELECT LAST(Name) As Name FROM
customer
Output
Name
|
Chalee Angel
|
10. 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
|
Sample1 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 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
|
ที่มา : http://www.thaicreate.com/
ไม่มีความคิดเห็น:
แสดงความคิดเห็น