- ORDER BY 1
ORDER BY 1 表示按select的第一个字段排序
mysql> select id,name from employee
-> order by 1;
在这个例子中就是以 id 字段来排序。
- ORDER BY n
ORDER BY n 表示,以“第n个字段”排序。
可在n的后面加 升序 ASC 降序 DESC 关键字。
例子,
mysql> SELECT * FROM employee ORDER BY 1 ASC;
+----+-------+---------+
| id | name | address |
+----+-------+---------+
| 1 | carol | japan |
| 2 | bob | tokyo |
| 3 | alice | china |
+----+-------+---------+
3 rows in set (0.01 sec)
mysql> SELECT id,name FROM employee ORDER BY 1 DESC;
+----+-------+
| id | name |
+----+-------+
| 3 | alice |
| 2 | bob |
| 1 | carol |
+----+-------+
3 rows in set (0.00 sec)
mysql> SELECT id,name FROM employee ORDER BY 2;
+----+-------+
| id | name |
+----+-------+
| 3 | alice |
| 2 | bob |
| 1 | carol |
+----+-------+
3 rows in set (0.00 sec)
以上。