คำสั่ง SQL
รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language เป็นภาษาที่ใช้ในการติดต่อกับฐานข้อมูลหรือพูดอีกอย่างก็คือ เป็นภาษาที่ใช้ในการสั่งให้ฐานฐานข้อมูลกระทำการใด ๆ ตามคำสั่งที่เราสั่ง ซึ่งในการติดต่อฐานข้อมูลนั้น ไม่ว่าจะเป็น SQL Server , Microsoft Access , MySQL ,DB2 หรือแม้แต่ Oracle ก็จะต้องใช้คำสั่งภาษา SQL ในการควบคุมทั้งสิ้น และเราจะมาเรียนรู้ถึงคำสั่งพื้นฐาน ของ SQL ที่จำเป็นกัน
แต่ก่อนอื่นต้องทราบศัพท์ที่ใช้เรียกในตารางฐานข้อมูลก่อนนะครับสำหรับใครที่ยังไม่รู้จักคำว่า ฟิลด์(Field) และ เรกคอร์ด(Record) ผมไม่อธิบายมากนะเอาเป็นว่าดูตามรูปก็แล้วกัน
โดยส่วนใหญ่แล้วการใช้คำสั่ง SQL เพื่อติดต่อฐานข้อมูลนั้น จะใช้โดยหลักคือ 3 กรณี
1. การเรียกดู
2. การแก้ไข ลบ, เพิ่ม, เปลี่ยนแปลง
3. การสร้างขึ้นใหม่
สำหรับตัวอย่างในบทเรียนนี้ ผมได้สร้าง Table ขึ้นมา 3 Table ครับ
1.customer เป็นตารางเก็บข้อมูลลูกค้า
2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า
3.country เป็นตารางเก็บข้อมูลประเทศ
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
|
Table : country
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United states
|
** ที่มา http://www.thaicreate.com/tutorial/sql-database-introduction.html
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 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
** ที่มา http://www.thaicreate.com/tutorial/sql-count.html
SQL CREATE TABLE
เป็นคำสั่งที่ใช้สำหรับการสร้างตารางใหม่ (Create New Table) ในฐานข้อมูล
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE "TableName"
(
ColumnName1 DataType ,
ColumnName2 DataType ,
...
)
Sample1 ทำการสร้างตาราง customer ประก้อบด้วยฟิวด์ฟิวด์CustomerID,Name,Email,CountryCode,Budget,Used
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
)
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
** ที่มา http://www.thaicreate.com/tutorial/sql-create-table.html
SQL MIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
** ที่มา http://www.thaicreate.com/tutorial/sql-min.html
SQL MAX
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
** ที่มา http://www.thaicreate.com/tutorial/sql-max.html
SQL DISTINCT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
** ที่มา http://www.thaicreate.com/tutorial/sql-distinct.html
SQL UNIQUE
เป็นคำสั่งที่ใช้สำหรับการสร้าง UNIQUE ต้องการให้ฟิวด์หรือ Column นั้น ๆ มีค่าไม่ซ้ำกันของ Rows ทั้งหมด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE "TableName"
(
ColumnName1 DataType ,
ColumnName2 DataType ,
ColumnName3 DataType ,
ColumnName4 DataType ,
...
UNIQUE (ColumnName1,ColumnName2...)
)
หรือในกรณี ALTER
ALTER TABLE "TabmeName" ADD UNIQUE (Column1,Column2)
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 1 ต้องการทำ UNIQUE ของ Table customer สำหรับ Column ชื่อ Email (กรณีแก้ไข Table)
ALTER TABLE customer ADD UNIQUE (Email)
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
|
EN
|
3000000
|
600000
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
เมื่อทำ UNIQUE แล้ว Column Email จะไม่สามารถมีค่าซ้ำกันของ Rows ทั้งหมด
** ที่มา http://www.thaicreate.com/tutorial/sql-unique.html
SQL JOIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
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
|
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 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
|
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 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 การเลือกข้อมูลแบบเชื่อมตาราง 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
|
** ที่มา http://www.thaicreate.com/tutorial/sql-join.html
SQL SUM
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
** ที่มา http://www.thaicreate.com/tutorial/sql-sum.html
SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (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
|
Sample1 การเลือกข้อมูลที่ระบุฟิวด์
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
|
Sample2 การเลือกข้อมูลทั้งหมดของ Table
SELECT * FROM customer
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
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
** ที่มา http://www.thaicreate.com/tutorial/sql-select.html
SQL RAND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
|
** ที่มา http://www.thaicreate.com/tutorial/sql-rand.html