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

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Analysis of Mysql transaction characteristics and level principles

1. What is a transaction? A database transaction ...

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

JS implements the sample code of decimal conversion to hexadecimal

Preface When we write code, we occasionally encou...

Use and analysis of Mysql Explain command

The mysql explain command is used to show how MyS...

Enable sshd operation in docker

First, install openssh-server in docker. After th...

A "classic" pitfall of MySQL UPDATE statement

Table of contents 1. Problematic SQL statements S...

Detailed tutorial on installing MySQL offline on CentOS7

1. Delete the original mariadb, otherwise mysql c...

Optimize the storage efficiency of BLOB and TEXT columns in InnoDB tables

First, let's introduce a few key points about...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

Linux nohup command principle and example analysis

nohup Command When using Unix/Linux, we usually w...

mySql SQL query operation on statistical quantity

I won't say much nonsense, let's just loo...