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

Công dụng của mệnh đề ORDER BY trong MySQL


Mệnh đề ORDER BY chỉ định thứ tự sắp xếp kết quả truy vấn. Phải theo sau tên cột cần sắp xếp.

Ví dụ

Bảng ratelist:

mysql> Select * from ratelist;
+----+------+-------+
| Sr | Item | Price |
+----+------+-------+
|  1 | A    |   502 |
|  2 | B    |   630 |
|  3 | C    |  1005 |
|  4 | h    |   850 |
|  5 | T    |   250 |
+----+------+-------+

Sắp xếp theo Price:

mysql> Select * from ratelist ORDER BY Price;
+----+------+-------+
| Sr | Item | Price |
+----+------+-------+
|  5 | T    |   250 |
|  1 | A    |   502 |
|  2 | B    |   630 |
|  4 | h    |   850 |
|  3 | C    |  1005 |
+----+------+-------+
5 rows in set (0.01 sec)

Kết quả sắp xếp tăng dần (mặc định) theo Price. Dùng ORDER BY Price DESC cho giảm dần.