如何使用 SQL Order By 语句对不区分大小写的结果进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2413427/
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
How to use SQL Order By statement to sort results case insensitive?
提问by CodeFusionMobile
I have a SQLite database that I am trying to sort by Alphabetical order. The problem is, SQLite doesn't seem to consider A=a during sorting, thus I get results like this:
我有一个 SQLite 数据库,我试图按字母顺序对其进行排序。问题是,SQLite 在排序过程中似乎没有考虑 A=a,因此我得到如下结果:
A B C T a b c g
A B C T a b c g
I want to get:
我想得到:
A a b B C c g T
A a b B C c g T
What special SQL thing needs to be done that I don't know about?
需要做哪些我不知道的特殊 SQL 事情?
SELECT * FROM NOTES ORDER BY title
回答by dan04
You can also do ORDER BY TITLE COLLATE NOCASE
.
你也可以这样做ORDER BY TITLE COLLATE NOCASE
。
Edit: If you need to specify ASC
or DESC
, add this after NOCASE
like
编辑:如果您需要指定ASC
or DESC
,请在NOCASE
like之后添加
ORDER BY TITLE COLLATE NOCASE ASC
or
或者
ORDER BY TITLE COLLATE NOCASE DESC
回答by Chad Birch
You can just convert everything to lowercase for the purposes of sorting:
您可以将所有内容转换为小写以进行排序:
SELECT * FROM NOTES ORDER BY LOWER(title);
If you want to make sure that the uppercase ones still end up ahead of the lowercase ones, just add that as a secondary sort:
如果您想确保大写字母仍然排在小写字母之前,只需将其添加为次要排序:
SELECT * FROM NOTES ORDER BY LOWER(title), title;
回答by Md Shahriar
SELECT * FROM NOTES ORDER BY UPPER(title)