No cache version.


Caching disabled. Default setting for this page:enabled (code DEF204)
If the display is too slow, you can disable the user mode to view the cached version.

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:

  1. mysql> SELECT name, birth FROM pet ORDER BY birth;
  2. +----------+------------+
  3. | name     | birth      |
  4. +----------+------------+
  5. | Buffy    | 1989-05-13 |
  6. | Bowser   | 1989-08-31 |
  7. | Fang     | 1990-08-27 |
  8. | Fluffy   | 1993-02-04 |
  9. | Claws    | 1994-03-17 |
  10. | Slim     | 1996-04-29 |
  11. | Whistler | 1997-12-09 |
  12. | Chirpy   | 1998-09-11 |
  13. | Puffball | 1999-03-30 |
  14. +----------+------------+

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:

  1. mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
  2. +----------+------------+
  3. | name     | birth      |
  4. +----------+------------+
  5. | Puffball | 1999-03-30 |
  6. | Chirpy   | 1998-09-11 |
  7. | Whistler | 1997-12-09 |
  8. | Slim     | 1996-04-29 |
  9. | Claws    | 1994-03-17 |
  10. | Fluffy   | 1993-02-04 |
  11. | Fang     | 1990-08-27 |
  12. | Bowser   | 1989-08-31 |
  13. | Buffy    | 1989-05-13 |
  14. +----------+------------+

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:

  1. mysql> SELECT name, species, birth FROM pet
  2.        ORDER BY species, birth DESC;
  3. +----------+---------+------------+
  4. | name     | species | birth      |
  5. +----------+---------+------------+
  6. | Chirpy   | bird    | 1998-09-11 |
  7. | Whistler | bird    | 1997-12-09 |
  8. | Claws    | cat     | 1994-03-17 |
  9. | Fluffy   | cat     | 1993-02-04 |
  10. | Fang     | dog     | 1990-08-27 |
  11. | Bowser   | dog     | 1989-08-31 |
  12. | Buffy    | dog     | 1989-05-13 |
  13. | Puffball | hamster | 1999-03-30 |
  14. | Slim     | snake   | 1996-04-29 |
  15. +----------+---------+------------+

The DESC keyword applies only to the column name immediately preceding it (birth); it does not affect the species column sort order.


Find a PHP function

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

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

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.

Contents Haut