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'
หรือ แบบ 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'
หรือ แบบ 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 |
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 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 ที่มีค่าตั้งแต่ 1000000 - 3000000
SELECT * FROM customer WHERE Budget BETWEEN '1000000' AND '3000000'
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 |
US
| 3000000 | 600000 |
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 |
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
|
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
|
SQL DROP
เป็นคำสั่งที่ใช้สำหรับการลบคุณสมบัติต่าง ๆ ของตาราง เช่น ลบตาราง , ลบฐานข้อมูล , ลบ IndexDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
- ลบฐานข้อมูล
DROP TABLE 'TableName'
- ลบตาราง
DROP DATABASE 'DatabaseName'
- ลบ Index
DROP INDEX TableName.IndexName (for SQL Server)
DROP INDEX TableName on IndexName (for Access)
DROP TABLE 'TableName'
- ลบตาราง
DROP DATABASE 'DatabaseName'
- ลบ Index
DROP INDEX TableName.IndexName (for SQL Server)
DROP INDEX TableName on IndexName (for Access)
MYSQL ROUND()
เป็นคำสั่งของ MySQL ใช้ในการปัดเศษทศนิยม ตามต้องการDatabase : MySQL
Syntax
ROUND(expression,decimal)
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
SELECT ROUND(123.456,2)
Output
123.47
|