Last Updated: February 25, 2016
·
4.116K
· dmtrs

Order by enum in mysql

Consider you have the following enum field in a mysql table.

my_enum_field enum('default','c', 'a', 'b')

Querying the table like the following:

SELECT * FROM my_table ORDER BY my_enum_field ASC;

The result order will be in the order of the enum indexes and not the value meaning that the records with.
myenumfield</code> value default</code> will appear before the the rest with second coming the c</code> value. In order to get the enum alphabetic order you need to cast to a string like

SELECT * FROM my_table ORDER BY CAST(my_enum_field AS CHAR) ASC;