Last active
December 23, 2015 07:59
-
-
Save xak2000/6604221 to your computer and use it in GitHub Desktop.
MySQL version of php natsort()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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()).