Rechercher dans le manuel MySQL
3.3.4.4 Sorting Rows
You may have noticed in the preceding examples that the result
rows are displayed in no particular order. It is often easier
to examine query output when the rows are sorted in some
meaningful way. To sort a result, use an ORDER
BY
clause.
Here are animal birthdays, sorted by date:
- +----------+------------+
- | name | birth |
- +----------+------------+
- | Buffy | 1989-05-13 |
- | Bowser | 1989-08-31 |
- | Fang | 1990-08-27 |
- | Fluffy | 1993-02-04 |
- | Claws | 1994-03-17 |
- | Slim | 1996-04-29 |
- | Whistler | 1997-12-09 |
- | Chirpy | 1998-09-11 |
- | Puffball | 1999-03-30 |
- +----------+------------+
On character type columns, sorting—like all other
comparison operations—is normally performed in a
case-insensitive fashion. This means that the order is
undefined for columns that are identical except for their
case. You can force a case-sensitive sort for a column by
using BINARY
like so:
ORDER BY BINARY
.
col_name
The default sort order is ascending, with smallest values
first. To sort in reverse (descending) order, add the
DESC
keyword to the name of the column you
are sorting by:
- +----------+------------+
- | name | birth |
- +----------+------------+
- | Puffball | 1999-03-30 |
- | Chirpy | 1998-09-11 |
- | Whistler | 1997-12-09 |
- | Slim | 1996-04-29 |
- | Claws | 1994-03-17 |
- | Fluffy | 1993-02-04 |
- | Fang | 1990-08-27 |
- | Bowser | 1989-08-31 |
- | Buffy | 1989-05-13 |
- +----------+------------+
You can sort on multiple columns, and you can sort different columns in different directions. For example, to sort by type of animal in ascending order, then by birth date within animal type in descending order (youngest animals first), use the following query:
- +----------+---------+------------+
- | name | species | birth |
- +----------+---------+------------+
- | Chirpy | bird | 1998-09-11 |
- | Whistler | bird | 1997-12-09 |
- | Claws | cat | 1994-03-17 |
- | Fluffy | cat | 1993-02-04 |
- | Fang | dog | 1990-08-27 |
- | Bowser | dog | 1989-08-31 |
- | Buffy | dog | 1989-05-13 |
- | Puffball | hamster | 1999-03-30 |
- | Slim | snake | 1996-04-29 |
- +----------+---------+------------+
The DESC
keyword applies only to the column
name immediately preceding it (birth
); it
does not affect the species
column sort
order.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-sorting-rows.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.