Character Sequence of MySQL order by in

Source: Internet
Author: User

Select ID from a where ID in (111, 1,111) order by instr ('11, ', ID ))

In this case, there will be errors, which need to be solved through the following methods

There are two methods:

1. select ID from a where ID in (111, 111) order by instr (',', Concat (',', ID ,',')) note that you need to construct more numbers.

2. (recommended) Select * From 'mytable' where 'id' in (111, 111) order by field ('id) // It seems that the result is not correct when I test this statement.

 

YesArticleNote:

Field (STR, str1, str2, str3 ,...)
Returns the index (position) of str in the str1, str2, str3,... list. returns 0 if STR is not found.

Sorting Process: Find the ID of the selected records in the field list, and return the location based on the position.

This usage will lead to using filesort, which is a very inefficient sorting method. Unless the data changes frequently or has a long cache, we do not recommend that you sort the data in this way.

 

The author suggests thatProgramCodeIt is sorted by itself, but I did not find any performance bottleneck during the test.

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.