Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fcaldarelli/1ee7ea87064a7b6cdf80 to your computer and use it in GitHub Desktop.
Save fcaldarelli/1ee7ea87064a7b6cdf80 to your computer and use it in GitHub Desktop.
Order database results with specific field values
-- Specific order clause
-- In PostgreSQL can be used CASE ... WHEN in order clause:
ORDER BY
CASE field_to_order
WHEN 'value1' THEN 1
WHEN 'value2' THEN 2
WHEN 'value3' THEN 3
ELSE 4
END,
other_field_to_order
-- In MySQL can be used also FIELD() function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment