开发者

MySQL Order By Symbol Preference

开发者 https://www.devze.com 2022-12-24 15:32 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消