MySQL Order By Multi-Field Sorting Rules Code Example

MySQL Order By Multi-Field Sorting Rules Code Example

Say it in advance

On a whim, I want to know what rules are used for MySQL order by sorting? Okay, without further ado, let’s get straight to the point.

MySql order by single field

Create a test table as follows:

CREATE TABLE `a` ( 
 `code` varchar(255) DEFAULT NULL, 
 `name` varchar(255) DEFAULT NULL 
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT into a values('中一', ​​'我'); 
INSERT into a values('中二', '你'); 
INSERT into a values('高一', '我是'); 
INSERT into a values('高二', '我们'); 
INSERT into a values('高二', '我的');
 

The test statement is as follows:

-- hex(): Get hexadecimal bytecode select name, hex(name) from a order by name desc

The results are as follows:

name hex(name)
mine E68891E79A84
I am E68891E698AF
us E68891E4BBAC
I E68891
you E4BDA0

Obviously, the sorting in MySQL is based on bytecode. When the first word is the same, the bytecode of the second word is compared, and so on.

MySql order by multiple fields

Only with comparison can we have ideas and make progress. Therefore, we first list the descending sort results of a single field, and then look at the descending sort results of two fields, so that we can analyze the reason.

-- Sort in descending order by name select * from a order by name desc; 
-- Sort in descending order by code select * from a order by code desc;

The left side is the result of order by name desc, and the right side is the result of order by code desc

code name
Second year of high school mine
Grade 1 I am
Second year of high school us
Secondary 1 I
Secondary 2 you
code name
Second year of high school us
Second year of high school mine
Grade 1 I am
Secondary 2 you
Secondary 1 I

The result is obvious: when a single field is sorted, the order in which other fields appear is naturally sorted.

Let's look at the sorting of multiple fields.

-- Sort in descending order by code, name select * from a order by code, name desc;

code name
Secondary 1 I
Secondary 2 you
Grade 1 I am
Second year of high school mine
Second year of high school us

The results are as follows: First of all, thank you qq_27837327 and MjayTang. I tested it here. The original text saying that this sql sort is invalid is wrong. In fact, order by code, name desc is equivalent to order by code asc, name desc

After testing, it was found that the sorting effect of select * from a order by code and name desc is still invalid.

Let’s look at the following statement

-- Sort in descending order by code and name select * from a order by code desc, name desc; 
-- The effect of this statement is equivalent to the following statement, where 1 and 2 correspond to code and name respectively 
select code, name from a order by 1 desc, 2 desc; 

code name
Second year of high school mine
Second year of high school us
Grade 1 I am
Secondary 2 you
Secondary 1 I

Comparing the descending sorting of single fields of code and name, we can find that when using order by code desc, name desc, MySQL will first sort by code in descending order, and then use name for descending sorting based on the code in descending order.

In addition, we can also use the contat function to concatenate multiple fields and then sort them. But make sure the field cannot be null. Let's take a look at the concat sql statement and results.

select * from a order by concat(code,name) desc 

code name
Second year of high school mine
Second year of high school us
Grade 1 I am
Secondary 2 you
Secondary 1 I

Obviously, in this test example, order by concat(code, name) desc is equivalent to order by code desc, name desc

Summarize

The above is the full content of this article. I hope it will be helpful to everyone. Welcome to refer to: Several important MySQL variables, Detailed code explanation of the relationship between MySQL master library binlog (master-log) and slave library relay-log, Detailed explanation of MySQL prepare principle, etc. If you have any questions, please leave a message at any time. Everyone is welcome to communicate and discuss.

You may also be interested in:
  • MySQL Order by statement usage and optimization detailed explanation
  • In-depth analysis of the order by and group by sequence issues in MySQL
  • How to use union and order by at the same time in MySQL
  • MySQL Order By Syntax Introduction
  • MySQL Order By Index Optimization Method
  • MySQL order by performance optimization method example
  • Detailed discussion of the character order of mysql order by in (recommended)
  • Implementation analysis of MySQL ORDER BY
  • MySQL Order By Rand() Efficiency Analysis
  • Summary of three ways to implement ranking in MySQL without using order by

<<:  Videojs+swiper realizes Taobao product details carousel

>>:  How to modify the default encoding of mysql in Linux

Recommend

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...

What are the core modules of node.js

Table of contents Global Object Global objects an...

Native JS to achieve image marquee effects

Today I will share with you a picture marquee eff...

The iframe refresh method is more convenient

How to refresh iframe 1. To refresh, you can use j...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

Linux ssh server configuration code example

Use the following terminal command to install the...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

Example code for implementing a QR code scanning box with CSS

We usually have a scanning box when we open the c...

Summary of Linux command methods to view used commands

There are many commands used in the system, so ho...

The front end creates and modifies CAD graphics details through JavaScript

Table of contents 1. Current situation 2. Create ...