Home>Article>Database> How to use order by in mysql

How to use order by in mysql

青灯夜游
青灯夜游 Original
2022-06-15 14:16:37 12211browse

The "order by" keyword in mysql is mainly used to sort the data in the query results in a certain order, using the syntax "ORDER BY field name [ASC|DESC]"; "ASC" is the default value , indicating that the fields are sorted in ascending order, and "DESC" indicating that the fields are sorted in descending order. "ORDER BY" specifies multiple fields for sorting, and the multiple field names are separated by commas, which will be sorted from left to right in the order of the fields; when there is a null value in the sorted field, the null value will be treated as a minimum value.

How to use order by in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

MySQLORDER BY: Sort query results

ORDER BYKeywords are mainly used To sort the data in the query results in a certain order. The syntax format is as follows:

ORDER BY 字段名 [ASC|DESC]

The syntax description is as follows.

  • Field name: Indicates the name of the field that needs to be sorted. Multiple fields are separated by commas.

  • ASC|DESC:ASCmeans the fields are sorted in ascending order;DESCmeans the fields are sorted in descending order. Among them,ASCis the default value.

When using theORDER BYkeyword, you should pay attention to the following aspects:

  • ORDER BYThe keyword can be followed by a subquery (subqueries will be explained in detail in later tutorials, just learn about it here).

  • When there is a null value in the sorted field,ORDER BYwill treat the null value as the minimum value.

  • ORDER BYWhen specifying multiple fields for sorting, MySQL will sort from left to right according to the order of the fields.

Single field sorting

The following uses a specific example to illustrate how MySQL sorts query results when ORDER BY specifies a single field. .

Example 1

The following queries all records in the tb_students_info table and sorts the height field

mysql> SELECT * FROM tb_students_info ORDER BY height;

How to use order by in mysql

Multiple field sorting

The following uses a specific example to illustrate how MySQL sorts the query results when ORDER BY specifies multiple fields.

Example 2

Query the name and height fields in the tb_students_info table, sort by height first

mysql> SELECT name,height FROM tb_students_info ORDER BY height,name;

How to use order by in mysql

Note: When pairing multiple When fields are sorted, the first field to be sorted must have the same value before the second field is sorted. If all values in the first field's data are unique, MySQL will no longer sort the second field.

By default, the query data is sorted in ascending alphabetical order (A ~ Z), but the sorting of data is not limited to this. You can also use DESC in ORDER BY to sort the query results in descending order (Z ~ A ).

[Related recommendations:mysql video tutorial]

The above is the detailed content of How to use order by in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn