MySQL uses find_in_set() function to implement where in() order sorting

MySQL uses find_in_set() function to implement where in() order sorting

This article introduces a tutorial about how to use the find_in_set() function to sort by the order in where in () in MySQL. I hope this tutorial can be helpful to you.

select * from table where id in ('783',' 769',' 814',' 1577',' 1769') 
order by find_in_set( id, '783, 769, 814, 1577, 1769' )

Check out:

769
1577
814
1769
783

Why not the order 783 769 814 1577 1769?

Note: After searching, it was found that the cause was in find_in_set. If there is a space in the second parameter of find_in_set, the order will be messed up, because MySQL will not trim the space character for you before querying.

so...

After removing spaces:

select * from table where id in ('783',' 769',' 814',' 1577',' 1769') 
order by find_in_set( id, '783,769,814,1577,1769' ) 

Note that it is just removed
Spaces in '783,769,814,1577,1769'

Check it out again:
783
769
814
1577
1769

So far, we have implemented the sorting using where in find_in_set. find_in_set can also implement multi-condition sorting.

Summarize

The above is all about how MySQL uses the find_in_set() function to implement where in() order sorting. Interested friends can refer to: MySQL database table partitioning precautions [recommended], several important MySQL variables, sql and MySQL statement execution order analysis, etc. If there are any deficiencies, you are welcome to leave a message to correct them. I hope this helps you all.

You may also be interested in:
  • Detailed explanation of the use of find_in_set() function and in() in mysql
  • How to use the MySQL FIND_IN_SET function
  • 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 in MySQL
  • Basic usage of find_in_set function in mysql

<<:  Install Memcached and PHP Memcached extension under CentOS

>>:  How to build a drag and drop plugin using vue custom directives

Recommend

HTML data submission post_PowerNode Java Academy

The HTTP request methods specified by the HTTP/1....

How to use the vue timeline component

This article example shares the specific implemen...

How to run JavaScript in Jupyter Notebook

Later, I also added how to use Jupyter Notebook i...

Tips on MySQL query cache

Table of contents Preface Introduction to QueryCa...

How to expand the disk partition for centos system

Problem/failure/scenario/requirement The hard dis...

A simple tutorial on how to use the mysql log system

Table of contents Preface 1. Error log 2. Binary ...

Using JavaScript difference to implement a comparison tool

Preface At work, I need to count the materials su...

mysql 5.7.23 winx64 decompression version installation tutorial

Detailed installation tutorial of mysql-5.7.23-wi...

Install and configure ssh in CentOS7

1. Install openssh-server yum install -y openssl ...

vue-cli introduction and installation

Table of contents 1. Introduction 2. Introduction...

How to add double quotes in HTML title

<a href="https://www.jb51.net/" titl...

Various transformation effects of HTML web page switching

<META http-equiv="Page-Enter" CONTENT...

Detailed process of building nfs server using Docker's NFS-Ganesha image

Table of contents 1. Introduction to NFS-Ganesha ...