MySQL Order By Symbol Preference
I was wondering if there was a way to make alphanumeric charact开发者_开发百科ers have a higher order of preference than symbols when doing an ORDER BY.
I.E. 'a' to come before '('
There are many ways round this, but I'd prefer the most elegant db approach.
This will give you the required output.
SELECT <column_name>
FROM <table_name>
ORDER BY CASE
WHEN <column_name> REGEXP '^[a-z]' OR <column_name> REGEXP '^[A-Z]' THEN CONCAT('1',<column_name>)
WHEN <column_name> REGEXP '^[0-9]' THEN CONCAT('2',<column_name>)
ELSE CONCAT(3, <column_name>)
END;
It will first take up the alphabets, then numbers and lastly other characters.
精彩评论