Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of sorting by number size in mysql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the method of sorting mysql according to digital size, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Mysql can be sorted by number size. MySQL provides the keyword "ORDER BY" to sort the data in the query results in a certain order; the syntax format is "SELECT field name list FROM data table name ORDER BY field name list ASC | DESC;".

The data that meets the needs of users can be queried through conditional query statements, but the queried data is generally displayed in the order in which the data was originally added to the table. In order to make the order of query results meet the requirements of users, MySQL provides the ORDER BY keyword to sort the query results.

In practical applications, it is often necessary to sort the query results, for example, when shopping online, goods can be sorted according to price; in the hospital registration system, they can be sorted according to the order of registration and so on.

The ORDER BY keyword is mainly used to sort the data in the query results in a certain order. The syntax format is as follows:

SELECT Field name list FROM data Table name ORDER BY Field name list ASC | DESC

The syntax is explained as follows.

List of field names: indicates the names of the fields that need to be sorted. Multiple fields are separated by commas.

ASC | DESC:ASC: fields are sorted in ascending order; DESC: fields are sorted in descending order. Where ASC is the default value.

You should pay attention to the following aspects when using the ORDER BY keyword:

The ORDER BY keyword can be followed by a subquery (the subquery will be explained in more detail in later tutorials, which you can learn here).

When there is a null value in the sorted field, ORDER BY treats the null value as a minimum.

When ORDER BY specifies multiple fields to sort, MySQL sorts them from left to right in the order of the fields.

Example:

The following query all the records of the tb_students_info table and sort the height field. The SQL statement and run results are as follows.

Mysql > SELECT * FROM tb_students_info ORDER BY height +-+ | id | name | dept_id | age | sex | height | login_date | + -+-+ | 2 | Green | 3 | 23 | F | 2016 | 10-22 | 1 | Dany | 1 | 25 | F | 2015-09-10 | 4 | Jane | 1 | 22 | F | 2016-12-20 | 7 | Lily | 6 | F | 2016-02-26 | | 10 | | | Tom | 4 | 23 | M | 2016 | 08-05 | 8 | Susan | 4 | 23 | F | 170 | 10-01 | | 6 | John | 2 | 21 | M | 2015-11 | | 5 | Jim | 1 | 24 | M | 2016-01-15 | 9 | Thomas | 3 | 22 | M | 2016-06-| 07 | | 3 | Henry | 2 | 23 | M | 185 | 2015-05-31 | +-+ 10 rows in set (0.08 sec)

As you can see from the results, MySQL sorts the data of the query's height field in ascending order by numeric value.

These are all the contents of mysql's method of sorting by number size. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report