Sorting of in subqueries in MySQL

Source: Internet
Author: User

To perform a query in the past few days, You need to query in a specified result set, for example:

Select * From table_name where doc_id in ('1dba ', 'c20a', '907b ')

 

The doc_id list in the in Clause obtains a list of doc_id Constants by calling an external interface, and searches the local database for the data that matches the list.

Record. Later, I found that the result set returned by MySQL is sorted in the order of receiving the database, but I want to sort it in the order of doc_id list,

After Google's knowledge, we can change it:

 

Select * From table_name where doc_id in ('1dba ', 'c20a', '907b ') order by find_in_set ('1dba', 'c20a ', '907b ')

The results are indeed achieved.

 

I'm amazed at the wide and profound nature of SQL. I need to study it carefully ......

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.