ORDER BY alphabet first then follow by number

只谈情不闲聊 提交于 2019-11-26 22:23:10

问题


I looking for some tweak in mysql ordering , I normally select record from table and then order the record by Name(varchar) ASC but the number is always come first

here some example of my question (note. mysql sort the record with 0-9 first)

SELECT name FROM list ORDER BY name ASC
record returned:
1 star
2 star
9 slice
Ape
Age
Beg
Bell
Fish
Zoo

What i want is the alphabet order come first then follow by number

Desired output

Ape
Age
Beg
Bell
Fish
Zoo
1 star
2 star
9 slice

回答1:


Use the following ORDER BY clause:

ORDER BY IF(name RLIKE '^[a-z]', 1, 2), name



回答2:


Ref this

SELECT name FROM list ORDER BY name * 1 ASC

Edited

SELECT name FROM list ORDER BY name * 1, name ASC



回答3:


You can try something like this:

SELECT 
    name 
FROM 
    list 
ORDER BY 
    IF(name REGEXP '^[0-9]', CONCAT('zz',name),name) ASC

So if your name start with a digit you concatenate 'zz' in the beginning (so that it will be last)




回答4:


Try this..

It simple one to get your answer

SELECT name  from list ORDER BY (name +0) ASC ,name ASC


来源:https://stackoverflow.com/questions/17418215/order-by-alphabet-first-then-follow-by-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!