Home > Database > Mysql Tutorial > body text

How to use order by in mysql

青灯夜游
Release: 2022-06-15 14:16:37
Original
12434 people have browsed it

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.

MySQL ORDER BY: Sort query results

ORDER BY Keywords 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]
Copy after login

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: ASC means the fields are sorted in ascending order; DESC means the fields are sorted in descending order. Among them, ASC is the default value.

When using the ORDER BY keyword, you should pay attention to the following aspects:

  • ORDER BY The 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 BY will treat the null value as the minimum value.

  • ORDER BY When 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;
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template