简单排序
mysql> select * from user order by user.id;
+----+-------+
| id | name |
+----+-------+
| 1 | user1 |
| 2 | user2 |
| 3 | user3 |
| 4 | user4 |
| 5 | user5 |
+----+-------+
5 rows in set (0.00 sec)
指定排序方向
搭配desc
mysql> select * from user order by user.id desc;
+----+-------+
| id | name |
+----+-------+
| 5 | user5 |
| 4 | user4 |
| 3 | user3 |
| 2 | user2 |
| 1 | user1 |
+----+-------+
5 rows in set (0.00 sec)
控制输出记录数
- 在给出order by子句时,应该保证它位于from语句之后。如果使用limit,它必须位于order by之后。使用子句的次序将产生错误信息。
mysql> select * from user order by user.id desc limit 2;
+----+-------+
| id | name |
+----+-------+
| 5 | user5 |
| 4 | user4 |
+----+-------+
2 rows in set (0.00 sec)