วันพฤหัสบดีที่ 4 ธันวาคม พ.ศ. 2557

คำสั่ง SQL

คำสั่ง SQL
1. SQL SELECT INTO 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยใช้การเลือกข้อมูลจากต้นทางไปยังปลายทาง นิยมใช้สำหรับการ Copy Table หรือทำการ Backup Table
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... INTO [New-Table] 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 การเลือกข้อมูลตาราง customer เพื่อไปสำรองไว้ที่ customer_backup
SELECT * INTO customer_backup FROM customer

Output
Table : customer_backup
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



2.SQL UNION 
เป็นคำสั่งที่ใช้สำหรับการรวมหลาย Query มารวมให้ใน Table เดียวกับ โดยจำนวน คอลัมบ์หรือฟิวด์นั้นจะต้องเท่ากันด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,... FROM [Table-Name]
UNION
SELECT Column1,Column2,... 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

Table : country
CountryCode
CountryName
TH
Thailand
EN
English
US
United states

Sample1 การรวมข้อมูลของตาราง customer และ country
SELECT CustomerID,Name FROM customer
UNION
SELECT CountryCode,CountryName FROM country


Output

CustomerID
Name
C001
Win Weerachai
C002
John Smith
C003
Jame Born
C004
Chalee Angel
TH
Thailand
EN
English
US
United states


3.SQL UPDATE 
เป็นคำสั่งที่ใช้สำหรับแก้ไขข้อมูลในตาราง (Table) โดยสามารถทำการแก้ไขได้หลายฟิวด์และหลาย Record ภายในคำสั่ง 1 คำสั่ง ทั้งนี้ขึ้นอยู่กับ Where ที่ผู้ใช้ได้เขียนขึ้น
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
UPDATE [Table-Name] SET Column1='Value1',Column2='Value2',... WHERE clause

Table : country
CountryCode
CountryName
TH
Thailand
EN
English
US
United states
CH
Chaina

Sample1 การแก้ไขข้อมูลลงใน Table 
UPDATE country SET CountryCode = 'JP',CountryName='Japan' WHERE CountryCode = 'CH'

Output

CountryCode
CountryName
TH
Thailand
EN
English
US
United states
JP
Japan


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


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

CountCustomerID
4


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

CountryCode
TH
EN
US


7.SQL INSERT 
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง (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

Sample1 การเพิ่มข้อมูลลงใน Table 
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')

Output

CountryCode
CountryName
TH
Thailand
EN
English
US
United states
CH
Chaina


8.SQL LENGTH
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับข้อความในตำแหน่งที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT LENGTH(Name) As MyLength FROM customer

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 
SELECT LENGTH(Name) AS MyLength FROM customer

Output

MyLength
13
11
9
12


9.SQL SUBSTRING 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดข้อความในตำแหน่งที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT SUBSTR(Name,0,2) As MySubStr FROM customer

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 การเลือกข้อมูลโดยทำการตัดตำแหน่งที่ 1 ถึง
SELECT SUBSTR(Name,1,3) AS MySubStr FROM customer
Output

MySubStr
Win
Joh
Jam
Cha


Sample2 การเลือกข้อมูลโดยทำการตัดตำแหน่งจากสุดท้ายไปทางซ้าย 3 หลัก และเริ่มนับไปทางขวาอีก      3 หลัก 
SELECT SUBSTR(Name,-3,3) AS MySubStr FROM customer

Output

MySubStr
hai
ith
orn
gel


 10.SQL IN 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกเฉพาะค่าที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] IN ('Value1','Value2')
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 = C002 และ C003 
SELECT * FROM customer WHERE CustomerID IN ('C002','C003')

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