Home » » คำสั่ง SQL พร้อมตัวอย่าง

คำสั่ง SQL พร้อมตัวอย่าง

Written By Mr.Wichawuttha Thailand on วันพุธที่ 17 ธันวาคม พ.ศ. 2557 | 05:09

SQL JOIN 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ JOIN จะกระทำเมื่อมีข้อมูลตั้งแต่ 2 Table ขึ้นไป โดยข้อมูลเหล่านั้นเป็นข้อมูลที่มีความสัมพันธ์และเชื่อมโยงกับข้อมูลหลัก

Database : MySQL,Microsoft Access,SQL Server,Oracle 

Syntax

SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1],[Table-Name2]
WHERE [Table-Name1].Column = [Table-Name2].Column


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 

SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
4
C002
2008-08-02
400000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
5
C002
2008-08-07
100000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
6
C002
2008-08-15
300000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
7
C003
2008-08-20
400000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
8
C003
2008-08-25
200000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
9
C004
2008-07-04
100000


Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 

SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000


Sample3 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit 

SELECT audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'

Output 

AuditID
CustomerID
Date
Used
1
C001
2008-08-01
100000
2
C001
2008-08-05
200000
3
C001
2008-08-10
300000
...................................................................................................................................................................

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

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 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 RAND() LIMIT 2

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
...................................................................................................................................................................

SQL RIGHT JOIN 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ RIGHT JOIN จะทำการเลือกข้อมูลหลักและข้อมูลเชื่อมโยงที่สัมพันธ์กัน โดยจะทำการอิงจาก Table สองสำคัญก่อน ถ้าไม่มีข้อมูลใน Table แรก ข้อมูล Table สองก็ยังถูกสนใจและนำมาแสดงผล

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1]
RIGHT JOIN [Table-Name2] ON [Table-Name1].Column = [Table-Name2].Column


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
C006
Superman Returnsupermain.return@thaicreate.com
US
20000000

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
10
C005
2008-07-04
200000


Sample1 การเลือกข้อมูลแบบ RIGHT JOIN ตาราง customer และ audit 

SELECT customer.*,audit.* FROM customer
RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
4
C002
2008-08-02
400000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
5
C002
2008-08-07
100000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
6
C002
2008-08-15
300000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
7
C003
2008-08-20
400000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
8
C003
2008-08-25
200000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
9
C004
2008-07-04
100000
NULL
NULL
NULL
NULL
NULL
NULL
10
C005
2008-07-04
200000


Sample2 การเลือกข้อมูลแบบ RIGHT JOIN ตาราง customer และ audit และ CustomerID = C005 

SELECT customer.*,audit.* FROM customer
RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE audit.CustomerID = 'C005'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
NULL
NULL
NULL
NULL
NULL
NULL
10
C005
2008-07-04
200000


Sample3 การเลือกข้อมูลแบบ RIGHT JOIN ตาราง customer และ audit และ CustomerID = C005 และแสดงผลเฉพาะตาราง audit 

SELECT audit.* FROM customer
RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID
WHERE audit.CustomerID = 'C005'

Output 

AuditID
CustomerID
Date
Used
10
C005
2008-08-07
200000
...................................................................................................................................................................

SQL NOT LIKE 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด และไม่แสดง Record ที่ค้นพบ ซึ่งทำหน้าที่ตรงข้ามกับ LIKE 

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Filed] NOT LIKE '%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 การเลือกข้อมูลตารางที่ฟิวด์ Name มีไม่มีคำว่า ee อยู่ 

SELECT * FROM customer WHERE Name NOT LIKE '%ee%'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000


Sample2 การเลือกข้อมูลตารางที่ฟิวด์ Email ไม่มีคำว่า j นำหน้า 

SELECT * FROM customer WHERE Name NOT LIKE 'j%'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample3 การเลือกข้อมูลตารางที่ฟิวด์ Name ไม่มีคำว่า i ลงท้าย

SELECT * FROM customer WHERE Name NOT LIKE '%i'

Output 

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

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 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 ที่ไม่ซ้ำกัน 

SELECT DISTINCT CountryCode FROM customer

Output 

CountryCode
TH
EN
US
...................................................................................................................................................................

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 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 สูงที่สุด 

SELECT MAX(Budget) AS MaxBudget FROM customer

Output 

MaxBudget
4000000
...................................................................................................................................................................

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT REPLACE(Field,Search,Replace) AS [New-Field] 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


Sample1 การเลือกข้อมูลโดยทำการแทนที่ค่า Win ด้วย Mr.Win

SELECT REPLACE(Name, 'Win', 'Mr.Win') AS MyName FROM customer

Output 

MyName
Mr.Win Weerachai
John Smith
Jame Born
Chalee Angel


นอกนากนี้ยังสามารถทำ REPLACE ไปแทนที่ในส่วนของ WHERE อีกด้วยครับ
...................................................................................................................................................................

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

SELECT AVG(Budget) AS AvgBudget FROM customer

Output 

AvgBudget
2500000
...................................................................................................................................................................

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 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 การเลือกข้อมูลโดยนับความยาวในฟิวด์ Name 

SELECT LENGTH(Name) AS MyLength FROM customer

Output 

MyLength
13
11
9
12
Share this article :

0 ความคิดเห็น:

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

Follow us on Google+



 
Support : Your Link | Your Link | Your Link
Copyright © 2013. Overspace - All Rights Reserved
Template Created by Creating Website Modified by CaraGampang.Com
Proudly powered by Blogger