1.
SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง
(Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง
หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database:
MySQL, Microsoft Access,SQL Server,Oracle
Syntax
SELECT
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 การเลือกข้อมูลที่ระบุฟิลด์
SELECT CustomerID, Name, Email FROM customer
Output
CustomerID
|
Name
|
Email
|
C001
|
Win
Weerachai
|
win.weerachai@thaicreate.com
|
C002
|
John
Smith
|
john.smith@thaicreate.com
|
C003
|
Jame
Born
|
jame.born@thaicreate.com
|
C004
|
Chalee
Angel
|
chalee.angel@thaicreate.com
|
Sample2 การเลือกข้อมูลทั้งหมดของ
Table
SELECT *
FROM customer
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
|
C004
|
Chalee
Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
2. SQL WHERE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข
หรือมากกว่า 1 เงื่อนไข
Database : MySQL,Microsoft Access,SQL Server,Oracle
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'
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'
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
|