mysql implements where in of sequential sorting through find_in_set of function

  • 2021-09-11 21:43:19
  • OfStack

This article for you to introduce a on mysql implementation by where in () in the order of sorting, with find_in_set () function tutorial, 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' ) 

Find out:


769
1577
814
1769
783

Why not the order of 783 769 814 157 7 176 9?

Note: After searching, the reason is in find_in_set. If there is a space in the second parameter of find_in_set, the order will be out of order, because mysql will not give you trim space before querying.

so...

After going to the space:


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 only removed
Spaces in '783, 769, 814, 1577, 1769'


 Find out again :
783
769
814
1577
1769

So far, we have realized the sorting with where in find_in_set, and find_in_set can also realize multi-condition sorting

Summarize

The above is the whole introduction of where in () sequence sorting through find_in_set () function. Interested friends can refer to: MySQL database table partition precautions "Recommended", several important MySQL variables, sql and MySQL statement execution sequence analysis, etc. If there are shortcomings, please leave a message for correction. I hope it will be helpful to everyone.


Related articles: