Sometimes, in SQL, we need to order with a special sequence, like the order of an array.

For example, we have a collaborator list ordered by state, include accepeted, pending and disabled. The order rule is pending->accepted->disabled.

Mysql

In Mysql, we can use FIELD function.

e.g.

ORDER BY FIELD(state, 'pending', 'accepted', 'disabled')

PostgreSQL

But in Postgresql, we usually use CASE WHEN statment.

e.g.

ORDER BY
  CASE state
  WHEN 'pending' THEN 0
  WHEN 'accepted' THEN 1
  WHEN 'disabled' THEN 2
  ELSE 3
  END ASC

FYI: