I would like to retrieve an ordered query result, but I need to have some specific row(s) to be in front of the list. Something like here on Stack Overflow, in the list of answers the right answer is always the first one.
Assumed I need to have the rows with IDs 1,2,3 to be the head, the rest sorted by a date field, is it possible to do something like:
SELECT * FROM foo ORDER BY id IN (1,2,3), created_date
If not what is more efficient? There will be many rows!
SELECT *, 0 AS head FROM foo WHERE id IN (1,2,3)
UNION
SELECT *, 1 AS head FROM foo WHERE id NOT IN (1,2,3)
ORDER BY head, created_date
or
SELECT *, IF(id IN (1,2,3), 0, 1) AS head
ORDER BY head, created_date
(I use MySQL now, but I'm interested in any other SQL solution.)