SQL GROUP BY
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) โดยใช้หาผลรวมของคอลัมน์จากแถวใน Column
ที่ระบุและทำการรวม Group ภายใต้ Column
ที่อยู่หลัง GROUP BY
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT
Column,SUM(Column) FROM [Table-Name] GROUP BY Column
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
โดยแบ่ง Group ตาม CountryCode
SELECT
CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode
Output
|
CountryCode
|
SumBudget
|
|
EN
|
2000000
|
|
TH
|
1000000
|
|
US
|
7000000
|
ที่มา : http://www.thaicreate.com/tutorial/sql-group-by.html
เป็นคำสั่งที่ใช้สำหรับลบข้อมูลในตาราง (Table) โดยสามารถทำการลบได้หลาย Record ภายในคำสั่งเดียว หรือว่า Record เดียว
ทั้งนี้ขึ้นอยู่กับ Where ที่ผูใช้เขียนขึ้นด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
DELETE FROM [Table-Name] WHERE clause
Table : country
|
CountryCode
|
CountryName
|
|
TH
|
Thailand
|
|
EN
|
English
|
|
US
|
United states
|
|
JP
|
Japan
|
Sample1 การลบข้อมูลลงใน Table
DELETE FROM country WHERE CountryCode = 'JP'
Output
|
CountryCode
|
CountryName
|
|
TH
|
Thailand
|
|
EN
|
English
|
|
US
|
United states
|
ที่มา : http://www.thaicreate.com/tutorial/sql-delete.html
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql-insert.html
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql-update.html
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql-union.html
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql-distinct.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
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
1
|
C001
|
2008-08-01
|
100000
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
2
|
C001
|
2008-08-05
|
200000
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
3
|
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 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
|
Sample2 การเลือกข้อมูลโดยใช้ Operators
!= (ไม่เท่ากับ)
SELECT * FROM
customer WHERE CountryCode != 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM
customer WHERE CountryCode != 'US' and CountryCode != 'EN'
หรือจะใช้ or
SELECT * FROM
customer WHERE CountryCode != 'US' or Budget = '1000000'
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
|
|
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
|
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
|
ที่มา : http://www.thaicreate.com/tutorial/sql-where.html
SQL Database & Table
รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language
เป็นภาษาที่ใช้ในการติดต่อกับฐานข้อมูลหรือพูดอีกอย่างก็คือ
เป็นภาษาที่ใช้ในการสั่งให้ฐานฐานข้อมูลกระทำการใด ๆ ตามคำสั่งที่เราสั่ง
ซึ่งในการติดต่อฐานข้อมูลนั้น ไม่ว่าจะเป็น SQL Server , Microsoft Access , MySQL ,DB2 หรือแม้แต่
Oracle ก็จะต้องใช้คำสั่งภาษา SQL ในการควบคุมทั้งสิ้น และเราจะมาเรียนรู้ถึงคำสั่งพื้นฐาน ของ SQL ที่จำเป็นกัน
แต่ก่อนอื่นต้องทราบศัพท์ที่ใช้เรียกในตารางฐานข้อมูลก่อนนะครับสำหรับใครที่ยังไม่รู้จักคำว่า
ฟิลด์(Field) และ
เรกคอร์ด(Record)
โดยส่วนใหญ่แล้วการใช้คำสั่ง
SQL เพื่อติดต่อฐานข้อมูลนั้น จะใช้โดยหลักคือ 3 กรณี
1. การเรียกดู
2. การแก้ไข
ลบ, เพิ่ม, เปลี่ยนแปลง
3. การสร้างขึ้นใหม่
สำหรับตัวอย่างในบทเรียนนี้
ผมได้สร้าง Table ขึ้นมา
3 Table ครับ
1.customer เป็นตารางเก็บข้อมูลลูกค้า
2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า
3.country เป็นตารางเก็บข้อมูลประเทศ
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
|
Table : country
|
CountryCode
|
CountryName
|
|
TH
|
Thailand
|
|
EN
|
English
|
|
US
|
United states
|
ที่มา : http://www.thaicreate.com/tutorial/sql-database-introduction.html
ไม่มีความคิดเห็น:
แสดงความคิดเห็น