0

Sort query results in a particular order

Posted July 2nd, 2010 in by thomas

another useful mysql tip is to sort result in a particular order (not asc or desc)

imagine we have a table of animals, a field with the names and another with the type :  bird, dog and cat

in our result we want to see all cats at first then birds then dogs

we will use the ORDER BY FIELD(fieldname , ‘value1′, ‘value2′,…)

SELECT * FROM animals
 ORDER BY FIELD(type, 'cat',bird','dog')

Leave a Reply