Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created September 26, 2011 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasmiranda/1242653 to your computer and use it in GitHub Desktop.
Save douglasmiranda/1242653 to your computer and use it in GitHub Desktop.
SQL ORDER BY ignoring fields filled with "0"
-- This problem happens when you try to order columns ASC
-- and you want to ignore the 0 values in the first results
SELECT `DATABASE_NAME`.*
FROM `TABLE_NAME`
ORDER BY
CASE WHEN `FIELD_FILL_WITH_0` in("", "0") THEN 1
ELSE 0 END, `FIELD_FILL_WITH_0_OR_ANY_OTHER_FIELD` ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment