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

SQL HAVING


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

SELECT Column,AVG(Column) FROM [Table-Name] GROUP BY Column HAVING AVG(Column) [Condition]

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 และผลรวมของ Budget ของ CompanyCode >= 2000000

SELECT CountryCode,AVG(Budget) AS AvgBudget FROM customer GROUP BY CountryCode HAVING AVG(Budget) >= '2000000'

Output

CountryCode
AvgBudget
EN
2000000
US
3500000


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

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
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 NOT IN ('C002','C003')

Output

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


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

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

MaxBudget
4000000

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