Mysql select in by ID Sort implementation method _mysql

Source: Internet
Author: User
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

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.