Custom sorting of fields in MySQL

Source: Internet
Author: User

Today, the project encountered a problem where the SQL query results need to be sorted by field "Custom.


For example, the default value is 0, 1, and 2. The result is 1, 0, 2, 1, 2, and 0;

MySQL uses field ('field ',...) Note: The first field is the keyword, and 'field' is the field to be specified.


Select T. * From tbl_obj_order t order by field (status, 2, 3, 1, 0,-1)

Select T. * From tbl_obj_order t where status in (-, 3) Order by field (status,-1)


The results of the two methods are the same.


Notes:

1. Try to write the sorting rules (possible values of fields) in full !!

When order by field (...), if the following sorting field is not fully written:

A) if the data in the query result does not contain the omitted value (-1), the result will not be affected.

B). If the data in the query result contains the omitted value (-1), the result will be affected (the default method ).

 

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.