IN the past, it was very difficult to find out how to sort the values found in. Later, gg and bb found that mysql IN sorting is very simple. Next I will sort out the relevant instances for your reference.
Assume that a table is as follows:
Id |
Name |
1 |
Zhang San |
2 |
Li Si |
3 |
Wang Wu |
When we query multiple data records, the condition is generally a certain range.
For example:>, <, between and, IN
The Code is as follows: |
Copy code |
SELECT * FROM table_name WHERE id IN (2, 1, 3 ); |
The preceding SQL statement queries data with the id of 2, 1, and 3.
If you just want to query the data, there is no problem.
However, if you want to sort data, you will find that the data found by the SQL statement is as follows:
Id |
Name |
1 |
Zhang San |
2 |
Li Si |
3 |
Wang Wu |
That is to say, it is not sorted by IDS like 2, 1, and 3.
If you want to sort IN order, the SQL statement should be changed to the following:
The Code is as follows: |
Copy code |
SELECT * FROM table_name WHERE id IN (2, 1, 3) order by FIND_IN_SET (id, '2, 1, 3 ') |
The result is:
Id |
Name |
2 |
Li Si |
1 |
Zhang San |
3 |
Wang Wu |
This usage can extend a lot...