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

Example of using MySQL to count the number of different values ​​in a column

Preface The requirement implemented in this artic...

Mysql 5.6 "implicit conversion" causes index failure and inaccurate data

background When performing a SQL query, I tried t...

CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

Preface There are many devices nowadays, includin...

Detailed explanation of efficient MySQL paging

Preface Usually, a "paging" strategy is...

How to use JS WebSocket to implement simple chat

Table of contents Short Polling Long-Polling WebS...

Introduction to CSS style classification (basic knowledge)

Classification of CSS styles 1. Internal style --...

How to implement a simple HTML video player

This article introduces the method of implementin...

MySQL 5.6.27 Installation Tutorial under Linux

This article shares the installation tutorial of ...

Element UI table realizes drop-down filtering function

This article example shares the specific code for...

How to connect Django 2.2 to MySQL database

1. The error information reported when running th...

Zabbix monitors Linux hosts based on snmp

Preface: The Linux host is relatively easy to han...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...

js to realize automatic lock screen function

1. Usage scenarios There is such a requirement, s...