Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cách sắp xếp kết quả MySQL theo thứ tự giảm dần

Cần chỉ định từ khóa DESC (viết tắt của DESCENDING) trong mệnh đề ORDER BY để sắp xếp giảm dần.

Cú pháp

SELECT column1, column2, ..., columnN FROM table_name ORDER BY column1 [, column2, ...] DESC;

Ví dụ

Sắp xếp kết quả theo cột Id giảm dần:

mysql> SELECT * FROM Student ORDER BY Id DESC;
+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 17   | Raman   | Shimla  | Computers |
| 15   | Harshit | Delhi   | Commerce  |
| 2    | Aarav   | Mumbai  | History   |
| 1    | Gaurav  | Delhi   | Computers |
+------+---------+---------+-----------+
4 rows in set (0.00 sec)