Detailed example of sorting function field() in MySQL

Detailed example of sorting function field() in MySQL

Preface

In our daily development process, sorting is often used, and sometimes there is such a demand.

For example, you need to sort the query results according to the specific value of a field. As in the following example

The above is a personal information table. Suppose we want to sort it by 'seiki', 'iris', 'xut'. That is, name='seiki','iris','xut' are used for sorting.

What should we do when we encounter such a requirement? In fact, there is a MySQL function that can handle this requirement very conveniently.

That is the field() function

How to use it:

Among them, order by (str,str1,str2,str3,str4……) , str is compared with str1, str2, str3, str4, where str refers to the field name,

Meaning: The field str returns the queried result set in the order of strings str1, str2, str3, str4. If the str field value in the table does not exist in the record of str1, str2, str3, str4, it will be returned at the front of the result set.

Now that we know how to use it, let's implement the above requirements.

First, let’s sort in positive order

Or sort by 'seiki', 'iris', the results are as follows

Now let's sort 'seiki', 'iris' in reverse order

You can see that iris is at the front. Why is that? See the annotation on the picture above!

Well, this function is still very useful

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • A complete list of commonly used MySQL functions (classified and summarized)
  • Simple usage examples of MySQL custom functions
  • MYSQL custom function to determine whether it is a positive integer example code
  • Detailed explanation of the use of the built-in function locate instr position find_in_set in MySQL efficient fuzzy search
  • Detailed explanation of the use of find_in_set() function and in() in mysql
  • Detailed explanation of MySql Date function
  • Detailed explanation of MySQL query operation examples using aggregate functions
  • MySQL: mysql functions

<<:  Programs to query port usage and clear port usage in Windows operating system

>>:  Angular Dependency Injection Explained

Recommend

Detailed explanation of various ways to merge javascript objects

Table of contents Various ways to merge objects (...

Problems encountered in using MySQL

Here are some problems encountered in the use of ...

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

Some wonderful uses of URL objects in JavaScript

Table of contents Preface Parsing parameters Modi...

JavaScript Interview: How to implement array flattening method

Table of contents 1 What is array flattening? 2 A...

MYSQL updatexml() function error injection analysis

First, understand the updatexml() function UPDATE...

How to install nginx in centos7

Install the required environment 1. gcc installat...

Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

In the front-end and back-end separation developm...

Recommended tips for web front-end engineers

Let's first talk about the value of web front...

How to modify the default storage location of Docker images (solution)

Due to the initial partitioning of the system, th...

Why MySQL chooses Repeatable Read as the default isolation level

Table of contents Oracle Isolation Levels MySQL I...

One sql statement completes MySQL deduplication and keeps one

A few days ago, when I was working on a requireme...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...

How to limit the value range of object keys in TypeScript

When we use TypeScript, we want to use the type s...