Sort by ENUM Mysql value

Asked

Viewed 219 times

0

I have a question and since I did not find anything on the internet I decided to ask you, well, I have a Mysql table and this table contains an ENUM field that can be 'yes' or 'no', I wonder if it is possible to sort the results by these values, like this

SELECT * FROM tabela ORDER BY tabela.coluna = 'nao'

for the values shown as not to be presented first, the problem is that I can’t make it work with codeigniter, which I do?

1 answer

2


According to the documentation:

ENUM values are classified based on their indices, which depend on of the order in which the enumeration members were listed in the column specification. For example, 'not' before 'yes' for ENUM('not', 'yes'). Null or empty strings come before all other enumeration values.

To avoid problems with sorting ENUM columns, use:

  1. Declare the ENUM list in alphabetical order.
  2. Make sure that the column is sorted alphabetically and not by index number encoding ORDER BY CAST (coluna AS CHAR) or ORDER BY CONCAT (coluna).

Try to use :

ORDER BY CAST(tabela.coluna AS CHAR)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.