按字母顺序对 MySQL 结果进行排序,但数字最后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5861852/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:44:20  来源:igfitidea点击:

Sort MySQL results alphabetically, but with numbers last

mysqlsql-order-byalphabetical-sort

提问by Kzqai

Often, sorting is done with symbols sorted to the top, like 0or *or &. This is the default way that mysql sorts; numbers and symbols and then A-Z. However, that makes the often ugliest or most badly formatted results float to the top (e.g. a result of @#$@3423or 8 inchor &amp).

通常,排序是通过排序到顶部的符号完成的,例如0or*&。这是mysql排序的默认方式;数字和符号,然后是 AZ。然而,这使得通常最丑陋或格式最差的结果浮到顶部(例如@#$@3423or8 inch或的结果&amp)。

So I'd like to do a modified form of that, letters first A-Z, and then special characters last.

所以我想做一个修改过的形式,首先是字母 AZ,然后是特殊字符。

How would I go about creating that type of sort? Something in the ORDER BYclause?

我将如何创建这种类型的排序?东西的ORDER BY条款?

回答by Matthew

Based on a google-cached link to this page: http://www.google.com/url?sa=t&source=web&cd=3&ved=0CCUQFjAC&url=http%3A%2F%2Fblog.feedmarker.com%2F2006%2F02%2F01%2Fhow-to-do-natural-alpha-numeric-sort-in-mysql%2F&ei=Zg2_TZyKDaffiALjjqwo&usg=AFQjCNGS-rX7AmfrumXK8J7bVSj96bSSmQ

基于指向此页面的 google 缓存链接:http: //www.google.com/url?sa=t&source=web&cd=3&ved=0CCUQFjAC&url =http%3A%2F%2Fblog.feedmarker.com%2F2006%2F02% 2F01 %2Fhow-to-do-natural-alpha-numeric-sort-in-mysql%2F&ei=Zg2_TZyKDaffiALjjqwo&usg=AFQjCNGS-rX7AmfrumXK8J7bVSj96bSSmQ

EDIT: Original link is dead. Here is another link which actually explains what is happening better than the first link did:

编辑:原始链接已失效。这是另一个链接,它实际上比第一个链接更好地解释了正在发生的事情:

http://matthewturland.com/2008/11/05/natural-ordering-in-mysql/

http://matthewturland.com/2008/11/05/natural-ordering-in-mysql/

You might try this

你可以试试这个

SELECT names FROM your_table ORDER BY names + 0 ASC

回答by Thomas

Select ...
From ...
Order By Case When Col Like '[0-9]%' Then 1 Else 0 End Asc
    , Col

Another solution that would account for special characters:

另一种解决特殊字符的解决方案:

Select ...
From ...
Order By Case When Col Like '[A-Z]%' Then 0 Else 1 End Asc
    , Col

回答by Gillskad gillskad

This works for me:

这对我有用:

SELECT * FROM `yourTable` ORDER BY `yourDatabase`.`yourColumn`  ASC