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 ).