How to sort the Where statement IN MySql

Source: Internet
Author: User
Tags mysql in


MySQL in the batch query, the result of the in query and in the order of the values in the sequence is not consistent, for this MySQL also has a sort of way in. The specific query is as follows:

The first way, the order by field, query by field, example:


SELECT * FROM table where ID in (5,3,6,1) Order by field (id,5,3,6,1);

Sorting process: Finds the ID of the selected record in the FIELD list and returns the position, sorted by position.
Note: This usage can result in a Using filesort, which is a very inefficient sort of method. This is not recommended unless the data changes at a very low frequency or has a long cache.

The second way, order by Substring_index, which uses the Substring_index (str,delim,count) function, truncates the ID string in the in to sort, example:

SELECT * FROM table where ID into (51,3,6,5) Order by Substring_index (', ', replace (' 51,3,6,5 ', ', ', ',,, '), ', ', '), concat ( ', ', id, ', ', 1);

Sort procedure: Returns a substring of string str before the Delim of the separator that appears in count one. If Count is a positive number, returns all characters from the last (counting from the left) separator to the left. If count is a negative number, returns all characters from the last (counting from the right) separator to the right. This is sorted after the Substring_index result is finally queried.
Note: As a result of the interception method, if the following characters are in the containing relationship with the preceding characters such as 5 and 51 in the above example, this will result in the interception of unwanted results (splitting 51, rather than splitting commas) to make the sort error, so add the substitution method in the example above to replace the ', ' number in str with ' ,, ' and then the left and right sides plus ', ' and then in the processed string ', ' +id ', ' to truncate, there will be no interception error.

The Third Way, order by Find_in_set, this sort uses the Find_in_set (str,strlist) function, which is sorted according to the position of the STR in strlist by the number 1 to N, example:

SELECT * FROM table where ID into (5,3,6,1) Order by Find_in_set (ID, ' 5,3,6,1 ');

Sort procedure: String str in a list of strings consisting of n Strlist, the return value ranges from 1 to N. A list of strings is a string of strings separated by a number of ', ' symbols, and then sorted according to the key value of the returned string in the corresponding strlist.
Note: Because functions are mainly distinguished by ', ', there is no ', ' in the sort character.


The table structure is as follows:
Mysql> select * from test;
+----+-------+
| ID | name |
+----+-------+
| 1 | Test1 |
| 2 | Test2 |
| 3 | Test3 |
| 4 | test4 |
| 5 | Test5 |
+----+-------+

Execute the following sql:
Mysql> SELECT * FROM Test where ID in (3,1,5);
+----+-------+
| ID | name |
+----+-------+
| 1 | Test1 |
| 3 | Test3 |
| 5 | Test5 |
+----+-------+
3 Rows in Set (0.00 sec)

The result of this select in MySQL is automatically sorted by ID,
But I want to execute "SELECT * from Test where ID in (3,1,5);" The results are sorted in the condition of in, namely: 3,1,5,

The desired results are as follows:
ID Name
3 Test3
1 test1
5 TEST5

How to write in this SQL in MySQL?
SQL Server can be found on the Internet with the order by CHARINDEX solution, but did not see how the MySQL solution?? Please help the master, thank you

Thanks!

SELECT * from a-order by Substring_index (' 3,1,2 ', id,1);

Try this good,ls positive solution.


ORDER BY Find_in_set (ID, ' 3,1,5 ')

Thanks, the test order by Substring_index and by Find_in_set can be

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.