รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language  
 | 
แต่ก่อนอื่นต้องทราบศัพท์ที่ใช้เรียกในตารางฐานข้อมูลก่อนนะครับสำหรับใครที่ยังไม่รู้จักคำว่า ฟิลด์(Field) และ เรกคอร์ด(Record) ผมไม่อธิบายมากนะเอาเป็นว่าดูตามรูปก็แล้วกัน
โดยส่วนใหญ่แล้วการใช้คำสั่ง SQL เพื่อติดต่อฐานข้อมูลนั้น จะใช้โดยหลักคือ 3 กรณี
1. การเรียกดู
2. การแก้ไข ลบ, เพิ่ม, เปลี่ยนแปลง
3. การสร้างขึ้นใหม่
สำหรับตัวอย่างในบทเรียนนี้ ผมได้สร้าง Table ขึ้นมา 3 Table ครับ
1.customer เป็นตารางเก็บข้อมูลลูกค้า
2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า
3.country เป็นตารางเก็บข้อมูลประเทศ
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 : 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 | 
Table : country
CountryCode 
 | 
CountryName 
 | 
|---|---|
TH 
 | Thailand | 
EN 
 | English | 
US 
 | United states | 
ตัวอย่างที่ 1
SQL ALIAS
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดย ALIAS คือการสร้างชื่อจำลองขึ้นมาใหม่ โดยสามารถจำลองชื่อได้ทั้งชื่อ
Field และชื่อ Table
Database : MySQL
Syntax
SELECT Column1 AS Alias1,Column2 AS Alias2,Column3 AS Alias3,... FROM [Table-Name1] Table Alias
Table : customer
CustomerID 
 | 
Name 
 | 
Emai 
 | 
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 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อ
ฟิวด์ขึ้นมาใหม่
SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer
Output
CusID 
 | 
CusName 
 | 
CusEmail 
 | 
|---|---|---|
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
C002 
 | John Smith | john.smith@thaicreate.com | 
C003 
 | Jame Born | jame.smith@thaicreate.com | 
C004 
 | Chalee Angel | chalee.angel@thaicreate.com | 
Sample2 การเลือกข้อมูลตาราง customer,audit โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
SELECT X.*,Y.* FROM customer X
LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID
WHERE X.CustomerID = 'C001'
LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID
WHERE X.CustomerID = 'C001'
Output
CustomerID 
 | 
Name 
 | 
Email 
 | 
CountryCode 
 | 
Budget 
 | 
Used 
 | 
AuditID 
 | 
CustomerID 
 | 
Date 
 | 
Used 
 | 
|---|---|---|---|---|---|---|---|---|---|
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
1 
 | 
C001 
 | 
2008-08-01 
 | 100000 | 
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
2 
 | 
C001 
 | 
2008-08-05 
 | 200000 | 
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
3 
 | 
C001 
 | 
2008-08-10 
 | 300000 | 
Sample3 การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อ Table เพื่อง่านต่อการเรียกใช้งาน
SELECT X.CustomerID,X.Name FROM customer X
Output
CusID 
 | 
CusName 
 | 
|---|---|
C001 
 | Win Weerachai | 
C002 
 | John Smith | 
C003 
 | Jame Born | 
C004 
 | Chalee Angel | 
ตัวอย่างที่ 2
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 | 
ตัวอย่างที่ 3
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 | 
ตัวอย่างที่ 4
SQL OR AND 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000
Output
ตัวอย่างที่ 5
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ
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 | 
ตัวอย่างที่ 5
SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่ระบุฟิวด์
Output
Sample2 การเลือกข้อมูลทั้งหมดของ Table
Output
ตัวอย่างที่ 6
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (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 | 
ตัวอย่างที่ 6
SQL ORDER BY 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยจัดเรียงข้อมูลตามต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย
Output
ตัวอย่างที่ 7
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 = มากไปหาน้อย
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 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย
SELECT * FROM customer ORDER BY CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC
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 | 
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 | 
C002 
 | John Smith | john.smith@thaicreate.com | 
EN 
 | 2000000 | 800000 | 
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
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 | 
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
C002 
 | John Smith | john.smith@thaicreate.com | 
EN 
 | 2000000 | 800000 | 
ตัวอย่างที่ 7
SQL LIMIT 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : MySQL
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
Output
ตัวอย่างที่ 8
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 | 
ตัวอย่างที่ 8
SQL RAND 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ในรูปแบบของการสุ่ม Record
Database : MySQL
Syntax
Table : customer
Sample1 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
Output
ตัวอย่างที่ 9
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 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 RAND() LIMIT 2
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 | 
ตัวอย่างที่ 9
SQL LIKE 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : customer
Sample1 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
Output
Sample2 การเลือกข้อมูลตารางที่ฟิวด์ Email มีคำว่า j นำหน้า
Output
Sample3 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า i ลงท้าย
Output
การใช้งาน LIKE ที่หลายเงื่อนไข หรือ OR ใน Statement สามารถใช้ [x,y] ได้
เช่น
ตัวอย่างที่ 10
SQL INSERT
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง (Table) โดยสามารถเพิ่มได้ทั้งแถวหรือว่าเพิ่มในส่วนของแต่ละฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : country
Sample1 การเพิ่มข้อมูลลงใน Table
Output
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Filed] LIKE '%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 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
SELECT * FROM customer WHERE Name LIKE '%ee%'
Output
CustomerID 
 | 
Name 
 | 
Email 
 | 
CountryCode 
 | 
Budget 
 | 
Used 
 | 
|---|---|---|---|---|---|
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
C004 
 | Chalee Angel | chalee.angel@thaicreate.com | 
US 
 | 4000000 | 100000 | 
Sample2 การเลือกข้อมูลตารางที่ฟิวด์ Email มีคำว่า j นำหน้า
SELECT * FROM customer WHERE Name LIKE 'j%'
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 | 
Sample3 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า i ลงท้าย
SELECT * FROM customer WHERE Name LIKE '%i'
Output
CustomerID 
 | 
Name 
 | 
Email 
 | 
CountryCode 
 | 
Budget 
 | 
Used 
 | 
|---|---|---|---|---|---|
C001 
 | Win Weerachai | win.weerachai@thaicreate.com | 
TH 
 | 1000000 | 600000 | 
การใช้งาน LIKE ที่หลายเงื่อนไข หรือ OR ใน Statement สามารถใช้ [x,y] ได้
เช่น
SELECT * FROM customer WHERE Name LIKE '%[John,jame]%'
ตัวอย่างที่ 10
SQL INSERT
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง (Table) โดยสามารถเพิ่มได้ทั้งแถวหรือว่าเพิ่มในส่วนของแต่ละฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
INSERT INTO [Table-Name] (Column1,Column2,Column3,...) VALUES ('Value1','Value2','Value3',...)
Table : country
CountryCode 
 | 
CountryName 
 | 
|---|---|
TH 
 | Thailand | 
EN 
 | English | 
US 
 | United states | 
Sample1 การเพิ่มข้อมูลลงใน Table
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
Output
CountryCode 
 | 
CountryName 
 | 
|---|---|
TH 
 | Thailand | 
EN 
 | English | 
US 
 | United states | 
CH 
 | Chaina | 

ไม่มีความคิดเห็น:
แสดงความคิดเห็น