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

Detailed explanation of JSON.parse and JSON.stringify usage

Table of contents JSON.parse JSON.parse Syntax re...

Detailed explanation of MySQL master-slave replication process

1. What is master-slave replication? The DDL and ...

Mysql backup multiple database code examples

This article mainly introduces the Mysql backup m...

JavaScript imitates Xiaomi carousel effect

This article is a self-written imitation of the X...

Detailed explanation of the order of JS object traversal

Some of you may have heard that the order of trav...

30 Tips for Writing HTML Code

1. Always close HTML tags In the source code of p...

HTML introductory tutorial HTML tag symbols quickly mastered

Side note <br />If you know nothing about HT...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

A brief analysis of how to upgrade PHP 5.4 to 5.6 in CentOS 7

1. Check the PHP version after entering the termi...

How does Vue track data changes?

Table of contents background example Misconceptio...