I recently came into a problem where I had to sort the result of a MySQL query by fixed column values.
Example::
SELECT *
FROM table
WHERE x in (a, d, c, b )
Result:: a b c d
The default ORDER BY clause in this case sorted the result in a descending fashion, however I need the result to be returned as follow.
Result:: a d c b
So how does one get such a result??
By using the the ORDER BY FIELD clause. This little nifty function allowed me to return the result exactly as hoped and did not require me to further manipulate the results afterwards using another programming language. Below is how you would structure this cool function.
Example::
SELECT *
FROM table
WHERE x in (a, d, c, b )
ORDER BY FIELD( fieldName, a, d, c, b )