How to define a custom ORDER BY order in mySQL

MySQL has a handy function called FIELD() which is excellent for tasks like this.

ORDER BY FIELD(Language,'ENU','JPN','DAN'), ID

Note however, that

  1. It makes your SQL less portable, as other DBMSs might not have such function

  2. When your list of languages (or other values to sort by) gets much longer, it’s better to have a separate table with sortorder column for them, and join it to your queries for ordering.

Leave a Comment