|
MariaDB [mydb]> select * from tbl1;
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 1 | tom | NULL | 1 | M |
| 2 | tao | NULL | 1 | M |
| 3 | xiu | NULL | 2 | M |
| 4 | wang | 25 | 5 | M |
+----+------+------+---------+--------+
4 rows in set (0.00 sec)
MariaDB [mydb]> select * from tbl1 order by ClassID; # 按 ClassID 升序排序
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 1 | tom | NULL | 1 | M |
| 2 | tao | NULL | 1 | M |
| 3 | xiu | NULL | 2 | M |
| 4 | wang | 25 | 5 | M |
+----+------+------+---------+--------+
4 rows in set (0.01 sec)
MariaDB [mydb]> select * from tbl1 order by ClassID,name; # 如果ClassID相同就按name升序排列
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 2 | tao | NULL | 1 | M |
| 1 | tom | NULL | 1 | M |
| 3 | xiu | NULL | 2 | M |
| 4 | wang | 25 | 5 | M |
+----+------+------+---------+--------+
4 rows in set (0.03 sec)
MariaDB [mydb]> select * from tbl1 order by ClassID,name DESC;# 如果ClassID相同就按name降序排列
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 1 | tom | NULL | 1 | M |
| 2 | tao | NULL | 1 | M |
| 3 | xiu | NULL | 2 | M |
| 4 | wang | 25 | 5 | M |
+----+------+------+---------+--------+
4 rows in set (0.02 sec)
MariaDB [mydb]> select * from tbl1 order by ClassID DESC; # 按ClassID降序排列
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 4 | wang | 25 | 5 | M |
| 3 | xiu | NULL | 2 | M |
| 1 | tom | NULL | 1 | M |
| 2 | tao | NULL | 1 | M |
+----+------+------+---------+--------+
4 rows in set (0.00 sec)
# 按ClassID降序排列,如果相同就按name降序排列
MariaDB [mydb]> select * from tbl1 order by ClassID DESC,name DESC;
+----+------+------+---------+--------+
| id | name | age | ClassID | gender |
+----+------+------+---------+--------+
| 4 | wang | 25 | 5 | M |
| 3 | xiu | NULL | 2 | M |
| 2 | tao | NULL | 1 | M |
| 1 | tom | NULL | 1 | M |
+----+------+------+---------+--------+
4 rows in set (0.00 sec)
|
|
|
|
|
|
|