วันจันทร์ที่ 1 ธันวาคม พ.ศ. 2557

คำสั่ง SQL

รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language

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


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

2.SQL WHERE 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข หรือมากกว่า 1 เงื่อนไข

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax
SELECT Column1, Column2, Column3,... FROM Table-Name WHERE [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


Sample1 การเลือกข้อมูลโดยใช้ Operators = (เท่ากับ)
SELECT * FROM customer WHERE CountryCode = 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode = 'US' and Budget = '4000000'

Output
CustomerID
Name
Email
CountryCode
Budget
Used
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000

3.SQL ALIAS 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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


Sample1 การเลือกข้อมูลตาราง 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

4.SQL OR AND 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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


Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000


SELECT * FROM customer WHERE CountryCode = 'US' AND Used = '100000'
หรือ
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

5.SQL ORDER BY 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Tableโดยจัดเรียงข้อมูลตามต้องการ

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax


SELECT Culumn1,Culumn2,Culumn3,... FROM [Table-Name] ORDER BY [Field] [ASC/DESC],[Field] [ASC/DESC],...
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 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย


SELECT * FROM customer ORDER BY CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC

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

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
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000

CustomerID
Name
Email
CountryCode
Budget
Used
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000
C004
Chalee Angel
chalee.angel@thaicreate.com
US
4000000
100000
C001
Win Weerachai
win.weerachai@thaicreate.com
TH
1000000
600000
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000



6.SQL SUB SELECT QUERY 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้เลือกข้อมูลย่อยภายใน SELECT ย่อยอีกชั้นหนึ่งครับSUB SELECT QUERY เข้ามาช่วยในด้านความสะดวกและง่ายกว่าการ JOIN TABLE แต่ข้อเสียของ SUB SELECT คือ สามารถทำงานได้ช้ากว่า JOIN TABLE

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax


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


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 ที่มีการใช้ยอดเงินในแต่ล่ะครั้งมากกว่า 400000


SELECT * FROM customer WHERE CustomerID IN (SELECT CustomerID FROM audit WHERE Used >= '400000')

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




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
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลผลรวมของ Budget

SELECT SUM(Budget) AS SumBudget FROM customer

Output 

SumBudget
10000000


8.MYSQL ROUND()
เป็นคำสั่งของ MySQL ใช้ในการปัดเศษทศนิยม ตามต้องการ

Database : MySQL

Syntax
ROUND(expression,decimal)


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
SELECT ROUND(123.456,2)

Output
123.47


9.MYSQL CEILING()
เป็นคำสั่งของ MySQL ใช้ในการปัดเศษทศนิยม ให้มีค่ามากขึ้น เช่น 2.01 จะปัดเป็น 3 หรือ 2.5 ก็จะได้เป็นค่า 3

Database : MySQL

Syntax
CEILING(expression)


Sample 1
SELECT CEILING(2.01)

Output
3


Sample 2
SELECT CEILING(2.5)

Output
3


สำหรับการใง้งานรวมกับ Column หรือ ฟิวด์ใน MySQL ก็สามารถทำการครอบ Column นั้น ๆ ได้เช่นเดียวกัน
SELECT Column1, CEILING(Column2) As AliasColumn2 FROM table_name


10.MYSQL INSTR()
เป็นคำสั่งของ MySQL ใช้ในการค้นหาตำแหน่งของข้อความ จากตำแหน่งแรกที่ค้นพบ

Database : MySQL

Syntax
INSTR(str,substr)


Sample
SELECT INSTR('foobarbar', 'bar');
-> 4

SELECT INSTR('xbar', 'foobar');
-> 0