MySQL 中的 ORDER BY 子句有什么用?

mysqlmysqli database

MySQL ORDER BY 子句用于指定查询结果的排序。关键字 ORDER BY 后面必须跟要排序的列的名称。例如,我们要根据列"price"对名为"ratelist"的下表进行排序 −

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

可以借助 ORDER BY 子句进行排序,如下所示 −

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)

上述查询根据价格按默认排序顺序(即升序)对表格进行排序。


相关文章