Database Reference
In-Depth Information
| 8 | Henry | 2014-08-01 | 197 |
| 2 | Suzi | 2014-07-29 | 391 |
| 7 | Suzi | 2014-08-02 | 502 |
+--------+-------+------------+-------+
Multiple-column sorts can be descending as well, but DESC must be specified after each
column name to perform a fully descending sort.
Multiple-column ORDER BY clauses can perform mixed-order sorting where some col‐
umns are sorted in ascending order and others in descending order. The following query
sorts by name in descending order, then by trav_date in ascending order for each name:
mysql> SELECT * FROM driver_log ORDER BY name DESC, trav_date;
+--------+-------+------------+-------+
| rec_id | name | trav_date | miles |
+--------+-------+------------+-------+
| 2 | Suzi | 2014-07-29 | 391 |
| 7 | Suzi | 2014-08-02 | 502 |
| 6 | Henry | 2014-07-26 | 115 |
| 4 | Henry | 2014-07-27 | 96 |
| 3 | Henry | 2014-07-29 | 300 |
| 10 | Henry | 2014-07-30 | 203 |
| 8 | Henry | 2014-08-01 | 197 |
| 5 | Ben | 2014-07-29 | 131 |
| 1 | Ben | 2014-07-30 | 152 |
| 9 | Ben | 2014-08-02 | 79 |
+--------+-------+------------+-------+
The ORDER BY clauses in the queries shown thus far refer to the sorted columns by name.
You can also name the columns by using aliases. That is, if an output column has an
alias, you can refer to the alias in the ORDER BY clause:
mysql> SELECT name, trav_date, miles AS distance FROM driver_log
-> ORDER BY distance;
+-------+------------+----------+
| name | trav_date | distance |
+-------+------------+----------+
| Ben | 2014-08-02 | 79 |
| Henry | 2014-07-27 | 96 |
| Henry | 2014-07-26 | 115 |
| Ben | 2014-07-29 | 131 |
| Ben | 2014-07-30 | 152 |
| Henry | 2014-08-01 | 197 |
| Henry | 2014-07-30 | 203 |
| Henry | 2014-07-29 | 300 |
| Suzi | 2014-07-29 | 391 |
| Suzi | 2014-08-02 | 502 |
+-------+------------+----------+
Search WWH ::




Custom Search