Skip to content

Instantly share code, notes, and snippets.

@xak2000
Last active December 23, 2015 07:59
Show Gist options
  • Save xak2000/6604221 to your computer and use it in GitHub Desktop.
Save xak2000/6604221 to your computer and use it in GitHub Desktop.
MySQL version of php natsort()
SELECT *
FROM %table_name%
ORDER BY
(CASE WHEN ASCII(RIGHT(%column_name%, 1)) > 47 AND ASCII(RIGHT(%column_name%, 1)) < 58
THEN LPAD(CONCAT(%column_name%, '-'), 5, '0')
ELSE LPAD(%column_name%, 5, '0')
END);
@xak2000
Copy link
Author

xak2000 commented Sep 18, 2013

Change %table_name% to actual table name and %column_name% to actual column name in table.
Use expression in parentheses each time you need a sorting by that column in natural order (like php natsort()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment