หน้าเว็บ

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

ตัวอย่างคำสั่งsql

1. 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 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 การเลือกข้อมูลที่ 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 Angelchalee.angel@thaicreate.com
US
4000000100000

2. 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 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 การเลือกข้อมูลโดยทำการจัดเรียงจาก 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 Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


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 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 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อฟิวด์ขึ้นมาใหม่

SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer

Output 

CusID
CusName
CusEmail
C001
Win Weerachaiwin.weerachai@thaicreate.com
C002
John Smithjohn.smith@thaicreate.com
C003
Jame Bornjame.smith@thaicreate.com
C004
Chalee Angelchalee.angel@thaicreate.com

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

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 Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] BETWEEN [Value-Start] AND [Value-End]


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 ที่มีค่าตั้งแต่ 1000000 - 3000000 

SELECT * FROM customer WHERE Budget BETWEEN '1000000' AND '3000000'

Output 

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.smith@thaicreate.com
US
3000000600000

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

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

7. 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 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 มากที่สุดออกมา 2 Record 

SELECT TOP 2 * FROM customer ORDER BY Budget DESC

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000

8. 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 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 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record 

SELECT * FROM customer ORDER BY Used DESC LIMIT 0,2

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000


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

Database : Oracle 

Syntax

SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE ROWNUM <= [Int-Limit]


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 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record 

SELECT * FROM customer WHERE ROWNUM <= 2 ORDER BY Used DESC

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000


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




ไม่มีความคิดเห็น:

แสดงความคิดเห็น