ตัวอย่าง database ดังนี้
Book_id
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2
|
Vb.net 2010
|
100
|
3
|
Vb.net 2011
|
200
|
4
|
Vb.net 2012
|
500
|
5
|
Vb.net 2013
|
500
|
1. คำสั่ง SQL นี้ ค้นหาข้อมูลง่ายๆ ที่ book_id = 1
" SELECT * FROM tbl_book WHERE book_id = 1 "
Book_id
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2. คำสั่ง SQL นี้ ค้นหาข้อมูล เรียงลำดับข้อมูล น้อยไปมาก
" SELECT * FROM tbl_book ORDER BY book_id "
Book_id
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2
|
Vb.net 2010
|
100
|
3
|
Vb.net 2011
|
200
|
4
|
Vb.net 2012
|
500
|
5
|
Vb.net 2013
|
500
|
3. คำสั่ง SQL นี้ ค้นหาข้อมูล เรียงลำดับข้อมูล มากไปน้อย
" SELECT * FROM tbl_book ORDER BY book_id DESC "
Book_id
|
Book_name
|
Book_price
|
5
|
Vb.net 2013
|
500
|
4
|
Vb.net 2012
|
500
|
3
|
Vb.net 2011
|
200
|
2
|
Vb.net 2010
|
100
|
1
|
Vb.net 2008
|
50
|
4. คำสั่ง SQL นี้ ค้นหาข้อมูล จัดกลุ่มข้อมูล
" SELECT book_price FROM tbl_book GROUP BY book_price "
book_price |
50
|
100
|
200
|
500
|
5. คำสั่ง SQL นี้ เพิ่มข้อมูล อีก 1 row
" INSERT INTO tbl_book (book_id , book_name, book_price) VALUES (6, 'AAA',20) "
Book_id
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2
|
Vb.net 2010
|
100
|
3
|
Vb.net 2011
|
200
|
4
|
Vb.net 2012
|
500
|
5
|
Vb.net 2013
|
500
|
6
|
AAA
|
20
|
6. คำสั่ง SQL แก้ไขข้อมูล
" UPDATE tbl_book SET
book_name = 'Vb.net' ,
book_price = 111
WHERE book_id = 1 "
Book_id
|
Book_name
|
Book_price
|
1
|
Vb.net
| 111 |
7. คำสั่ง SQL ลบข้อมูล
" DELETE FROM tbl_book WHERE book_id = 5 "
Book_num
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2
|
Vb.net 2010
|
100
|
3
|
Vb.net 2011
|
200
|
4
|
Vb.net 2012
|
500
|
6
|
AAA
|
20
|
8. คำสั่ง SQL นี้ สามารถหาผลรวมที่ต้องการได้
" SELECT * FROM tbl_book WHERE (book_id & 7) = book_id "
Book_num
|
Book_name
|
Book_price
|
1
|
Vb.net 2008
|
50
|
2
|
Vb.net 2010
|
100
|
4
|
Vb.net 2012
|
300
|
9. คำสั่ง SQL นี้ count นับจำนวน row
" SELECT count(*) as count_book FROM tbl_book "
count_book |
5 |
10. คำสั่ง SQL นี้ MAX
" SELECT max(book_price) as book_price FROM tbl_book "
count_book |
500 |
11. คำสั่ง SQL นี้ Update data and Select Data ถ้าจะใช้กับ Insert data and Select Data ก็ได้
" UPDATE tbl_book SET
book_name = 'Vb.net' ,
book_price = (SELECT max(book_price) as book_price FROM tbl_book)
WHERE book_id = 1 "
12. คำสั่ง SQL insert date time database
"INSERT INTO tbl_book
(book_date)
VALUES
(
CURRENT_TIMESTAMP
)"
12. คำสั่ง SQL insert date time database for vb.net
Dim date_now As Date = "#01/12/2014 11:11:11#"
"INSERT INTO tbl_book
(book_date)
VALUES
(
IIf( date_now = Date.MinValue, "Null", "STR_TO_DATE('" & Format(date_now , "M/d/y h:m:s") & "', '%m/%d/%Y %h:%i:%s')")
)"
13. ตัดตัวอักษร SQL Oracle
SELECT MAX(substr(book_id,2)) AS ID
FROM tbl_book ;
14. ตัดตัวอักษร SQL server
SELECT MAX(RIGHT(book_id,2)) AS ID
FROM tbl_book
GROUP BY RIGHT(book_id,2);
book_name = 'Vb.net' ,
book_price = (SELECT max(book_price) as book_price FROM tbl_book)
WHERE book_id = 1 "
12. คำสั่ง SQL insert date time database
"INSERT INTO tbl_book
(book_date)
VALUES
(
CURRENT_TIMESTAMP
)"
12. คำสั่ง SQL insert date time database for vb.net
Dim date_now As Date = "#01/12/2014 11:11:11#"
"INSERT INTO tbl_book
(book_date)
VALUES
(
IIf( date_now = Date.MinValue, "Null", "STR_TO_DATE('" & Format(date_now , "M/d/y h:m:s") & "', '%m/%d/%Y %h:%i:%s')")
)"
13. ตัดตัวอักษร SQL Oracle
SELECT MAX(substr(book_id,2)) AS ID
FROM tbl_book ;
14. ตัดตัวอักษร SQL server
SELECT MAX(RIGHT(book_id,2)) AS ID
FROM tbl_book
GROUP BY RIGHT(book_id,2);