MySQL ORDER BY 子句用于指定查询结果的排序。关键字 ORDER BY 后面必须跟有我们要排序的列的名称。例如,我们要根据“价格”列对名为“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)
上面的查询根据默认排序顺序(即升序)的价格对表格进行排序。