PostgreSQL で order by field
http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks
Source: http://stackoverflow.com/questions/1309624/simulating-mysqls-order-by-field-in-postgresql When we can explicitly to specify some order, we should to use function field in MySQL. select * from pet order by field(species, 'cat', 'dog', 'bird') desc; +----------+--------+---------+------+------------+------------+ | name | owner | species | sex | birthday | death | +----------+--------+---------+------+------------+------------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1990-08-27 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------+--------+---------+------+------------+------------+ This functionality is missing in PostgreSQL - we could to use CASE statement or we could to write own function. It's possible - we have variadic functions: select * from pet order by species, case species when 'cat' then 1 when 'dog' then 2 when 'bird' then 3 else 0 end desc;