วันอาทิตย์ที่ 14 ธันวาคม พ.ศ. 2557

คำสั่ง SQL

คำสั่ง SQL


         เป็นภาษาที่ใช้ในการติดต่อกับฐานข้อมูลหรือพูดอีกอย่างก็คือ เป็นภาษาที่ใช้ในการสั่งให้ฐานฐานข้อมูล                        กระทำการใด ๆ ตามคำสั่งที่เราสั่ง ซึ่งในการติดต่อฐานข้อมูลนั้น ไม่ว่าจะเป็น SQL Server , Microsoft Access ,MySQL ,              DB2  หรือแม้แต่ Oracle ก็จะต้องใช้คำสั่งภาษา SQL ในการควบคุมทั้งสิ้น

      โดยส่วนใหญ่แล้วการใช้คำสั่ง SQL เพื่อติดต่อฐานข้อมูลนั้น จะใช้โดยหลักคือ 3 กรณี ดังนี้
1. การเรียกดู
2. การแก้ไข ลบเพิ่มเปลี่ยนแปลง
3. การสร้างขึ้นใหม่

1. SQL SELECT
             
               เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้                                          ทั้งตารางหรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
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.smith@thaicreate.com
US
3000000
600000
C004
ChaleeAngel
chalee.angel@thaicreate.com
US
4000000
100000


Sample1 การเลือกข้อมูลที่ระบุฟิวด์ 
SELECT CustomerID, Name, Email FROM customer
Output
CustomerID
Name
Email
C001
Win eerachai
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
WinWeerachai
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


2. SQL LIMIT

                เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
WinWeerachai
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
WinWeerachai
win.weerachai@thaicreate.com
TH
1000000
600000



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
WinWeerachai
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
ChaleeAngel
chalee.angel@thaicreate.com
US
4000000
100000
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000


4. 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
WinWeerachai
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

5. SQL NOT IN 

               เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Tableโดยไม่เลือกเฉพาะ   
ค่าที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax

SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] NOT IN ('Value1','Value2')

Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
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 NOT IN ('C002','C003')

Output 
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
win.weerachai@thaicreate.com
TH
1000000
600000
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000




6.  SQL COPY TABLE   (CREATE TABLE... SELECT...)
             เป็นคำสั่งที่ใช้สำหรับสร้างตารางใหม่ โดยทำการ COPY/CREATE TABLE และข้อมูลจากตาราง                                     ที่มีอยู่แล้ว
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE [Table-Name] SELECT * FROM [Table-Name] WHERE ....

Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
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 การเพิ่มข้อมูลลงใน Table customer2 โดยการ SELECT จาก customer 
CREATE TABLE customer2 SELECT * FROM customer

Output (Table : customer2) 
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
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

7. 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
WinWeerachai
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

8. SQL AVG


           เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าเฉลี่ย
ผลรวมของฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax

SELECT AVG(Column/Field) AS [New-Field] FROM [Table-Name]

Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
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 AVG(Budget) AS AvgBudget FROM customer

Output 
AvgBudget
2500000



9. SQL FIRST

            เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Tableโดยเลือกข้อมูล แถวแรก    

ของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax

SELECT FIRST(ColumnName) FROM TableName

Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
WinWeerachai
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


10. 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
WinWeerachai
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
ChaleeAngel
chalee.angel@thaicreate.com
US
4000000
100000

Sample1 การเลือกข้อมูล Budget ต่ำที่สุด 
SELECT MIN(Budget) AS MinBudget FROM customer   

Output 
MinBudget
1000000
                                                                                                                                                                                          
                                                                                                                                                           ที่มา : http://www.thaicreate.com/