MySQL in sort can also be sorted in the order in inside

Source: Internet
Author: User

Sql:select * FROM table where ID in (3,9,6);
After such a situation is taken out, in fact, the ID or by 3,6,9, sorted, but if we really want to press in the order of the inside of what to do? Can SQL be completed? Do you need to take it back and then foreach it?
Actually, it can.
Sql:select * FROM table where ID in (3,9,6) Order by field (id,3,9,6);
The order that comes out is the specified (3,6,9) order.
About the efficiency of this sort,
Some articles indicate:
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: The ID of the selected record is searched in the FIELD list and returned to the location, sorted by position.
Such usage can lead to Using Filesort, which is a very inefficient sort method. This is not recommended unless the data changes frequently, or if there is a long cache.
Using the explain test results in a using Filesort.
It is recommended that you sort yourself in the program code.

MySQL in sort can also be sorted in the order in inside

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.