MySQL 先按字母排序,然后按数字排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17418215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ORDER BY alphabet first then follow by number
提问by Leon Armstrong
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
我在 mysql 排序中寻找一些调整,我通常从表中选择记录,然后按 Name(varchar) ASC 排序记录,但数字总是在前
here some example of my question (note. mysql sort the record with 0-9 first)
这里是我的问题的一些例子(注意。mysql 首先用 0-9 对记录进行排序)
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
回答by Barmar
Use the following ORDER BY
clause:
使用以下ORDER BY
子句:
ORDER BY IF(name RLIKE '^[a-z]', 1, 2), name
回答by Salil
回答by Stephan
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)
因此,如果您的名字以数字开头,则在开头连接 'zz'(以便它是最后一个)
回答by Ganesh Rengarajan
Try this..
尝试这个..
It simple one to get your answer
得到你的答案很简单
SELECT name from list ORDER BY (name +0) ASC ,name ASC