Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use order by sorting to retrieve data in Mysql

Shulou Source: shulou.com Published: 2022-06-03 11:02:14 09月20日 Update

This article is about how to use order by sorting to retrieve data in Mysql. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In myslq data, the retrieved data is often displayed in the order in which the underlying data is added to the table, but there may be update and delete operations, which will affect the sort order, and all the data queried from the data without specifying the sort order is meaningless. Here you need to use the Oderby clause to sort the data.

Mysql | orderby sort and retrieve data (ASC,DESC)

EG:

SELECT*FROMproductsORDERBYproducts.prod_priceDESC,products.prod_nameASC

Explanation:

DESC: stands for descending order.

ASC: stands for ascending order. (do not write the default value)

The query results are sorted in descending order according to products.prod_price, and then the records with the same products.prod_price are sorted according to the descending order of products.prod_name. If there are more sort fields separated by commas.

Note:

When sorting textual text, whether the sort is sorted according to case, such as whether an is before B or after Z, depends on the setting of the database, it is impossible to sort statements.

ORDERBYDESC instance

SQLORDERBY keyword

The ORDERBY keyword is used to sort the result set by one or more columns.

The ORDERBY keyword sorts records in ascending order by default. If you need to sort the records in descending order, you can use the DESC keyword.

SQLORDERBY syntax

SELECTcolumn_name,column_name

FROMtable_name

ORDERBYcolumn_name,column_nameASC | DESC

Demo database

In this tutorial, we will use the RUNOOB sample database.

The following SQL statement selects all sites from the "Websites" table and sorts them in descending order by the "alexa" column:

Case study

SELECT*FROMWebsites

ORDERBYalexaDESC

Thank you for reading! This is the end of this article on "how to use order by sorting to retrieve data in Mysql". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Sort data search keyword database more order representative content ascending order instance article result statement query good practical the same depending on Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft OPPO Reno Shulou Technology MySQL