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 realization method of asc and desc data sorting in MySQL

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

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail the implementation of asc and desc data sorting in MySQL. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Data sorting asc, desc

1. Single field sort order by field name

Function: which or which fields to sort by

Meaning: order by clause is used for sorting, and order by is followed by sorting fields. Multiple sorting fields can be placed with comma spacing. Order by uses ascending asc by default. If there is a where clause, order by must be placed after the where statement.

(1) sort according to the salary from small to large (the system defaults from small to big)

For example: select ename,sal from emp order by sal

(2) employees who get job as MANAGER will be sorted according to their salary (the system defaults from small to large)

For example: select ename,job,sal from emp where job = "MANAGER" order by sal

If you include the where statement order by must be placed after the where, if there is no where statement order by is placed after the table

(3) the following inquiry method is wrong:

Select * from emp order by sal whereselect * from emp order by sal where job = 'MANAGER'

2. Sort the fields manually.

(1) manually specify the order of salary from smallest to largest (ascending keyword asc)

For example: select ename,sal from emp order by sal asc

(2) manually specify the sort by salary from largest to smallest (descending keyword desc)

For example: select ename,sal from emp order by sal desc

3. Sort multiple fields

(1) sort by job and salary in reverse order

For example: select ename,job,ename from emp order by job desc,sal desc

Note: if multiple fields are used to sort, if the sort is repeated according to the first field, it will be sorted according to the second field.

4. Sort by field location

(1) sort by salary ascending order (this method is not recommended, the meaning of the number is not clear, the readability is not strong, and the program is not robust)

Select * from emp order by 6

This is the end of this article on "the implementation of asc and desc data sorting 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, please share it for more people to see.

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