Implementation of MySQL asc and desc data sorting

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc

1. Single field sorting order by field name

Function: Which field or fields to sort by

Meaning: Sorting uses the order by clause, followed by the sorting field. You can put multiple sorting fields, separated by commas. Order by default uses ascending order (asc). If there is a where clause, then order by must be placed after the where clause.

(1) Sort by salary from small to large (the system defaults to small to large)

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

這里寫圖片描述

(2) Get employees whose job is MANAGER and sort them by salary from small to large (the system defaults to small to large)

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

這里寫圖片描述
If a where clause is included, order by must be placed after where; if there is no where clause, 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. Manually specify field sorting

(1) Manually specify the sorting by salary from small to large (ascending keyword asc)

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

這里寫圖片描述

(2) Manually specify the sorting by salary from high to low (descending keyword desc)

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

這里寫圖片描述

3. Sorting multiple fields

(1) Sort by job and salary in descending order

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

這里寫圖片描述

Note: If multiple fields are used for sorting, if the sorting based on the first field is repeated, the sorting will be based on the second field;

4. Sort by field position

(1) Sort by salary in ascending order (this method is not recommended, as the meaning of the numbers is unclear, readability is poor, and the program is not robust)

select * from emp order by 6; 

這里寫圖片描述

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Database query sorting using random sorting results example (Oracle/MySQL/MS SQL Server)
  • MySQL query statement uses limit to limit the number of rows queried
  • Two methods to sort Chinese data in MySQL by pinyin
  • Basic tutorial on sorting data using indexes in MySQL
  • MYSQL Must Know Reading Notes Chapter 5 Sorting and Retrieving Data
  • Yii2 implements cross-MySQL database association query sorting function code
  • MySQL database index order by sorting detailed explanation
  • Introduction to MySQL limit query and data sorting

<<:  JavaScript implements the nine-grid mobile puzzle game

>>:  How to run a project with docker

Recommend

Detailed explanation of Linux mpstat command usage

1. mpstat command 1.1 Command Format mpstat [ -A ...

Detailed installation tutorial of zabbix 4.04 (based on CentOS 7.6)

1. Preparation before installation: 1.1 Install J...

Use js to call js functions in iframe pages

Recently, I have been working on thesis proposals ...

In-depth explanation of MySQL stored procedures (in, out, inout)

1. Introduction It has been supported since versi...

One line of code solves various IE compatibility issues (IE6-IE10)

x-ua-compatible is used to specify the model for ...

Tutorial on installing PHP on centos via yum

First, let me introduce how to install PHP on Cen...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

Common usage of hook in react

Table of contents 1. What is a hook? 2. Why does ...

Detailed explanation of the use of MySQL DML statements

Preface: In the previous article, we mainly intro...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...

Example of using CSS to achieve semi-transparent background and opaque text

This article introduces an example of how to use ...

Tutorial on how to quickly deploy a Nebula Graph cluster using Docker swarm

1. Introduction This article describes how to use...

Write a simple calculator using JavaScript

The effect is as follows:Reference Program: <!...