วันอังคารที่ 16 ธันวาคม พ.ศ. 2557

คำสั่ง SQL


คำสั่ง SQL


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
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 AVG(Budget) AS AvgBudget FROM customer


output
AvgBudget
2500000



ที่มา : http://www.thaicreate.com/tutorial/sql-avg.html



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


Sample2 การเลือกข้อมูลตาราง customer,audit โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน 
SELECT X.*,Y.* FROM customer X
LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID
WHERE X.CustomerID = 'C001'

Output 
CustomerID
Name
Email
Budget
Used
CustomerID
Date
Used
C001
Win Weerachai
win.weerachai@thaicreate.com
1000000
600000
C001
2008-08-01
100000
C001
Win Weerachai
win.weerachai@thaicreate.com
1000000
600000
C001
2008-08-05
200000
C001
Win Weerachai
win.weerachai@thaicreate.com
1000000
600000
C001
2008-08-10
300000


Sample3 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน 
SELECT X.CustomerID,X.Name FROM customer X

Output 
CusID
CusName
C001
Win Weerachai
C002
John Smith
C003
Jame Born
C004
Chalee Angel

ที่มา : http://www.thaicreate.com/tutorial/sql-alias.html




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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT MID(column_name,start[,length]) 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 เลือกข้อมูล Column ชือ่ Name ออกมา 4 ตัวอักษร

SELECT MID(Name,1,4) As Name FROM customer

Output

Name
Win
John
Jame
Chal

ที่มา : http://www.thaicreate.com/tutorial/sql-mid.html




SQL CONVERT
     
       เป็นคำสั่งที่ใช้ในแปลงข้อมูลของวันที่ให้อยู่ในรูปแบบต่าง ๆ ของฐานข้อมูล SQL Server 

Database : SQL Server

Syntax

CONVERT ( data_type [ ( length ) ] ,expression [ ,style ] )

เช่น

CONVERT (GETDATE() , 103)

ที่มา : http://www.thaicreate.com/tutorial/sql-convert.html




SQL ROUND
      
       เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการปัดเศษขึ้นในกรณีที่มากกว่า >= .5 และปัดเศษลงในกรณีที่น้อยกว่า < .5

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT ROUND(ColumnName) FROM table_name

ที่มา : http://www.thaicreate.com/tutorial/sql-round.html





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'

ที่มา : http://www.thaicreate.com/tutorial/sql-operators-or-and.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



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

ที่มา : http://www.thaicreate.com/tutorial/sql-limit.html



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

ที่มา : http://www.thaicreate.com/tutorial/sql-top.html




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

ที่มา : http://www.thaicreate.com/tutorial/sql-in.html