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

XHTML Getting Started Tutorial: Commonly Used XHTML Tags

<br />Just like an article, our web pages sh...

jQuery canvas draws picture verification code example

This article example shares the specific code of ...

How to change the CentOS server time to Beijing time

1. I purchased a VPS and CentOS system, and found...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

Summary of React's way of creating components

Table of contents 1. Create components using func...

XHTML three document type declarations

XHTML defines three document type declarations. T...

RGBA alpha transparency conversion calculation table

Conversion between rgba and filter values ​​under...

Summary of 10 must-see JavaScript interview questions (recommended)

1.This points to 1. Who calls whom? example: func...

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...

The process of SSH service based on key authentication in Linux system

As we all know, SSH is currently the most reliabl...