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 OR AND
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000
Output
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 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 = US และ Used = 100000
SELECT * FROM customer WHERE CountryCode = 'US' AND Used = '100000'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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 UNION
เป็นคำสั่งที่ใช้สำหรับการรวมหลาย Query มารวมให้ใน Table เดียวกับ โดยจำนวน คอลัมบ์หรือฟิวด์นั้นจะต้องเท่ากันด้วยDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,... FROM [Table-Name]
UNION
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
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 |
INCT 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
|
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 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 มากที่สุดออกมา 2 Record
SELECT TOP 2 * FROM customer ORDER BY Budget DESC
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
SQL HAVING
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย HAVING ใช้กำหนดเงื่อนไขการเลือกข้อมูลที่อยู่ภายหลังคำสั่ง GROUP BYDatabase : 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
|
SQL IN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกเฉพาะค่าที่กำหนดDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] 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 IN ('C002','C003')
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
|---|---|---|---|---|---|
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
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
|
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 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 การเลือกข้อมูลโดยทำการแทนที่ค่า Win ด้วย Mr.Win
SELECT REPLACE(Name, 'Win', 'Mr.Win') AS MyName FROM customer
Output
MyName
|
|---|
Mr.Win Weerachai
|
John Smith
|
Jame Born
|
Chalee Angel
|
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 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 AVG(Budget) AS AvgBudget FROM customer
Output
AvgBudget
|
|---|
2500000
|
SQL COPY TABLE (CREATE TABLE... SELECT...)
เป็นคำสั่งที่ใช้สำหรับสร้างตารางใหม่ โดยทำการ COPY/CREATE TABLE และข้อมูลจากตารางที่มีอยู่แล้วDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE [Table-Name] SELECT * FROM [Table-Name] WHERE ....
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 การเพิ่มข้อมูลลงใน Table customer2 โดยการ SELECT จาก customer
CREATE TABLE customer2 SELECT * FROM customer
Output (Table : customer2)
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 |
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)
(
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 |
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 TABLE 'TableName'
- ลบตาราง
DROP DATABASE 'DatabaseName'
- ลบ Index
DROP INDEX TableName.IndexName (for SQL Server)
DROP INDEX TableName on IndexName (for Access)
SQL MID
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดคำใน Colomn หรือ Field ที่ต้องการDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT MID(column_name,start[,length]) 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 เลือกข้อมูล Column ชือ่ Name ออกมา 4 ตัวอักษร
SELECT MID(Name,1,4) As Name FROM customer
Output
Name
|
|---|
Win
|
John
|
Jame
|
Chal
|
MYSQL REPEAT()
เป็นคำสั่งของ MySQL ใช้ในการแทนที่แสดงจำนวนข้อความ ตามจำนวน Loop ที่ต้องการDatabase : MySQL
Syntax
REPEAT(str,count)
Sample
SELECT REPEAT('MySQL', 3);
-> MySQLMySQLMySQL
SELECT REPEAT('ThaiCreate', 5);
-> ThaiCreateThaiCreateThaiCreateThaiCreateThaiCreate
-> MySQLMySQLMySQL
SELECT REPEAT('ThaiCreate', 5);
-> ThaiCreateThaiCreateThaiCreateThaiCreateThaiCreate
ไม่มีความคิดเห็น:
แสดงความคิดเห็น