Wednesday 4 November 2015

MySQL ordering results by specific field values

In MySQL we can sort the results in ascending or descending order very easily by using the ORDER BY clause. However, there are times when you want to sort the results in a specific order which cannot be done using the ASC or DSC. FIELD() of MySQL ORDER BY clause can be used to sort the results in a specific order.

You can use ORDER BY and FIELD function

SELECT * FROM table ORDER BY FIELD(ID,1,5,4,3)