mysql basic knowledge-ORDER BY

Author: Dian ordinary world 

Source: SQL database development

Today, the basic knowledge of mysql brings you ORDER BY, let's take a look at how to use ORDER BY!

ORDER BY function

The ORDER BY keyword is used to sort the result set.

ORDER BY keyword

The ORDER BY keyword is used to sort the result set in ascending order (ASC) or descending order (DESC).

The ORDER BY keyword sorts the records in ascending order (ASC) by default, and the default sorting does not need to write ASC.

If you need to sort the records in descending order, you can use the DESC keyword, which cannot be omitted.

ORDER BY syntax

SELECT column1, column2, ... FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Sample database

The following is the data in the "Customers" table:

ORDER BY example

The following SQL statement selects all customers from the "Customers" table and sorts them according to the "Province" column:

Examples:

SELECT * FROM Customers
ORDER BY 省份;

result:

The default sorting method here is ascending. The sorting rules here are sorted according to the ASCII rules of the pinyin first letter of the province. For example, the initial letter of Beijing is B, the initial letter of Guangdong Province is G, the initial letter of Hubei Province is H, the initial letter of Shanghai is S, and the initial letter of Zhejiang Province is Z. The above result is obtained after sorting. If the first letter of the sort is the same, the second letter is compared, and so on.

ORDER BY DESC example

The following SQL statement selects all customers from the "Customers" table and sorts them in descending order according to the "Province" column:

Examples:

SELECT * FROM Customers
ORDER BY 省份 DESC;

The result is:

After adding DESC, the sorting method becomes descending, and the sorting rule is opposite to ascending.

ORDER BY multi-column example

To make it easier for everyone to understand, we add two rows of data to the Customers table.

INSERT INTO `Customers` (customer ID, name, address, city, zip code, province) 
VALUES 
(6,'Song Yi','Huacheng Avenue 21','Guangzhou', '510000','Guangdong Province' ); 
INSERT INTO `Customers` (customer ID, name, address, city, zip code, province) 
VALUES 
(7,'Liu Er','Chang'an Avenue 121','Beijing', '100000','Beijing' );

The Customers table after adding data is as follows:

The following SQL statement selects all customers from the "Customers" table and sorts them by the "Province" and "Name" columns:

Example

SELECT * FROM Customers 
ORDER BY province, name;

The result is:

It can be seen that when sorting the provinces, there are two in Beijing and Guangdong. At this time, sort the pinyin initials of the name column. On the premise that the provinces are consistent, Liu Er's initial letter is L and Zhao Qi's initial letter is Z, so Liu Er is ranked before Zhao Qi. Similarly, Song Yi’s initials are S, and Wang Wu’s initials are W, so Song Yi is ranked before Wang Wu.

ORDER BY multi-column example 2

The following SQL statement selects all customers from the "Customers" table, sorts them in ascending order by "Province", and sorts them in descending order by the "Name" column:

SELECT * FROM Customers  
ORDER BY province ASC, name DESC;

The results are as follows:

After the names are sorted in descending order, the sort result of the names is opposite to the ascending order.

Note: If you want to sort multiple columns in descending order, you must use DESC after each column.

ORDER BY and LIMIT combination example

After using LIMIT, we can find the top few after sorting, for example, find out the top three customer information after sorting provinces in ascending order:

SELECT * FROM Customers
ORDER BY `省份`
LIMIT 3

The results show that:

The result only shows the information of the top three customers in order.

 

Guess you like

Origin blog.csdn.net/yoggieCDA/article/details/109094203