1

I was working with PHP and mysql a table was given and I was given a table to sort which had a enum field

 enum('Pending','Acive','INACTIVE');

I did not understand how the table was sorted?

2 Answers 2

2

Enumeration Sorting

ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a'). The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.

To prevent unexpected results when using the ORDER BY clause on an ENUM column, use one of these techniques:

  • Specify the ENUM list in alphabetic order.

  • Make sure that the column is sorted lexically rather than by index number by coding ORDER BY CAST(col AS CHAR) or ORDER BY CONCAT(col).

Reference: http://dev.mysql.com/doc/refman/5.6/en/enum.html#enum-sorting

Sign up to request clarification or add additional context in comments.

Comments

0

You can sort like this in Sql

ORDER BY CASE status
           WHEN 'Pending' THEN 1
           WHEN 'Acive' THEN 3
           ELSE 2
         END 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.