COMPUTER LEARNING CENTER
WWW.HUUKHANG.COM
5.2.4. Meänh ñeà Order by
Thoâng thöôøng, trong khi truy vaán maåu tin töø baûng döõ lieäu, keát quaû hieån thò caàn saép xeáp theo
chieàu taêng hay giaûm döïa treân kyù töï ALPHABET. Nhöng baïn cuõng coù theå saép xeáp theo moät tieâu chuaån
baát kyø, chaúng haïn nhö bieåu thöùc.
Khi saép xeáp döõ lieäu trình baøy trong keát quaû, caàn phaûi choïn tröôøng hay bieåu thöùc theo traät töï
taêng daàn hoaëc giaûm daàn.
Cuù phaùp cho meänh ñeà ORDER BY cuøng vôùi traïng thaùi taêng hay giaûm, öùng vôùi ASC saép xeáp
taêng daàn, DESC giaûm daàn.
Cuù phaùp coù daïng nhö sau:
Order by columnname DESC
Order by columnname1 + columnname2 DESC
Order by columnname ASC
Order by columnname1 ASC, columnname2 DESC
Ví duï 8-6: SELECT vôùi meänh ñeà Order by DESC
/*-- Giaûm daàn theo thôøi gian */
Select OrderID , OrderDate, CustID, Amount
From tblOrders
Where Amount >1000
Order by OrderDate DESC
Keát quaû traû veà nhö sau:
OrderID OrderDate CustID Amount
---------- ------------ ----------------------
17
2001-09-20 12
178.243
18
2001-09-20 12
2.78534
16
2001-09-19 12
398.798
15
2001-09-18 12
5.758.876
14
2001-09-17 12
5.539.647
12
2001-09-16 12
1.330
13
2001-09-16 12
1.585.563
31
2001-09-16 13 459.525
11
2001-09-15 11
1.401.803
28
2001-09-15 13 1.45200
Ví duï 8-7: SQL daïng SELECT vôùi meänh ñeà Order by vaø ASC
/*-- Taêng daàn theo thôøi gian */
Select OrderID , OrderDate, CustID, Amount
From tblOrders
Where Amount >1000
Order by OrderDate ASC
Keát quaû traû veà nhö sau
OrderID OrderDate
CustID Amount
---------- --------------------------- --------
01
2001-09-05
10
2.903.576
02
2001-09-05
10
48.168.567
03
2001-09-05
10
5.107.032
04
2001-09-08
10
2.355.537
05
2001-09-08
16
1.817.487
06
2001-09-10
16
26.000
19
2001-09-10
12
575.667
Giaùo vieân: Phaïm Höõu Khang