Volodymyr Levytskyi
Volodymyr Levytskyi

Reputation: 3432

How to order mysql rows in custom order?

I want to order mysql records by one column whose values start with N,Y,F,P,U. Type of column is VARCHAR. I want to order by column according to first letter of this column's values. Specific order is N - Y - F - P - U. So first record must be that one with some column's value N, the second record has that column's value starting with Y and so on.

How to order by first letter of column's value?

Upvotes: 0

Views: 88

Answers (1)

symcbean
symcbean

Reputation: 48357

SELECT
...
ORDER BY 
CASE SUBSTR(one_column,1,1)
   WHEN 'N' THEN 0
   WHEN 'Y' THEN 1
   WHEN 'F' THEN 2
   WHEN 'P' THEN 3
   WHEN 'U' THEN 4
   ELSE 5
END

...or join to lookup table of values, or use IF functions to map the char to a number.

Upvotes: 3

Related Questions