คำสั่ง SQL
SQL BETWEEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการเลือกเงื่อนไขที่อยู่ระหว่างค่าเริ่มต้นและค่าสิ้นสุด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample การเลือกข้อมูลที่ Budget ที่มีค่าตั้งแต่ 1000000 - 3000000
Output
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 |
Sample การเลือกข้อมูลที่ 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 WHERE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข หรือมากกว่า 1 เงื่อนไข
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample การเลือกข้อมูลโดยใช้ Operators = (เท่ากับ)
Output
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 |
Sample การเลือกข้อมูลโดยใช้ 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 |
SQL REPLACE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการแทนที่ข้อความที่พบในตำแหน่งที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample การเลือกข้อมูลโดยทำการแทนที่ค่า Win ด้วย Mr.Win
Output
SQL ALTER
เป็นคำสั่งที่ใช้สำหรับการปรับแก้ไขคุณสมบัติของตาราง เช่น เพิ่ม / ลบ / แก้ไข จำนวน Coulmn
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 เพิ่ม Column ชื่อ Msn หลัง Column ชื่อ Email
Output
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 |
Sample การเลือกข้อมูลโดยทำการแทนที่ค่า 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 ALTER
เป็นคำสั่งที่ใช้สำหรับการปรับแก้ไขคุณสมบัติของตาราง เช่น เพิ่ม / ลบ / แก้ไข จำนวน Coulmn
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
ALTER TABLE tablename ADD COLUMN (field1 type (size) (index1), ...)
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 ชื่อ Msn หลัง Column ชื่อ Email
ALTER TABLE customer ADD Msn VARCHAR( 100 ) NOT NULL AFTER Email
Output
CustomerID
|
Name
|
Email
|
Msn
|
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 | |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
SQL CONVERT()
เป็นคำสั่งที่ใช้ในแปลงข้อมูลของวันที่ให้อยู่ในรูปแบบต่าง ๆ ของฐานข้อมูล SQL Server
Database : SQL Server
Syntax
เป็นคำสั่งที่ใช้ในแปลงข้อมูลของวันที่ให้อยู่ในรูปแบบต่าง ๆ ของฐานข้อมูล SQL Server
Database : SQL Server
Syntax
CONVERT ( data_type [ ( length ) ] ,expression [ ,style ] )
เช่น
CONVERT (GETDATE() , 103)
เช่น
CONVERT (GETDATE() , 103)
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
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
SQL LIKE เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Filed] LIKE '%Value%'
Table : customer
Sample1 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
SELECT * FROM customer WHERE Name LIKE '%ee%'
Output
SQL DISTINCT เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการเลือกข้อมูลที่ซ้ำกันมาเพียงแค่ Record เดียว Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT DISTINCT Column1,Column2,Column3,... FROM [Table-Name]
Table : customer
Sample1 การเลือกข้อมูล CountryCode ที่ไม่ซ้ำกัน
SELECT DISTINCT CountryCode FROM customer
Output
SQL COUNT เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวน Count Record ที่ค้นพบ Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT COUNT(Column/Field) AS [New-Field] FROM [Table-Name]
Table : customer
Sample1 การเลือกข้อมูลจำนวนลูกค้าทั้งหมด
SELECT COUNT(CustomerID) AS CountCustomerID FROM customer
Output
SQL DROP เป็นคำสั่งที่ใช้สำหรับการลบคุณสมบัติต่าง ๆ ของตาราง เช่น ลบตาราง , ลบฐานข้อมูล , ลบ Index Database : 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)
SQL LEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวนตัวอักษร Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT LEN(ColumnName) FROM table_name
Table : customer
Sample1 เลือกข้อมูล Column ชื่อ Name โดยทำการนับจำนวนตัวอักษร
SELECT Name, LEN(Name) As LenName FROM customer
Output
|
ไม่มีความคิดเห็น:
แสดงความคิดเห็น