微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的PostgreSQL ORDER BY不区分大小写?

我在Debian上运行Postgres 9.4.4,我得到以下ORDER BY行为:
veure_test=# show LC_COLLATE;
 lc_collate  
-------------
 en_US.UTF-8
(1 row)

veure_test=# SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') ORDER BY 1;
 regexp_split_to_table 
-----------------------
 a
 A
 b
 c
 Capacitor
 CD
 d
 D
(8 rows)

和uname -a:

Linux ---- 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1 x86_64 GNU/Linux

但是,在我的iMac上,使用Postgres 9.3.4,我得到以下内容

veure_test=# show LC_COLLATE;
 lc_collate  
-------------
 en_US.UTF-8
(1 row)

veure_test=# SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') ORDER BY 1;
 regexp_split_to_table 
-----------------------
 A
 CD
 Capacitor
 D
 a
 b
 c
 d
(8 rows)

和uname -a:

Darwin ---- 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64

我为什么Debian版本似乎不区分大小写而OS X版本不是这样,我感到很困惑.我缺少什么,或者我需要提供哪些其他信息?

更新:在我的Mac上,pg_collat​​ion表显示我有一个en_US.UTF-8排序规则,但在Debian上,我有一个en_US.utf8排序规则.因此,在我的Mac上:

veure_test=# with foo as (
SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') as bar
   )
SELECT bar FROM foo
ORDER BY bar collate "en_US.UTF-8";                                                                                                                                                                                      
    bar    
-----------
 A
 CD
 Capacitor
 D
 a
 b
 c
 d
(8 rows)

在Debian上:

veure_test=# with foo as (
SELECT regexp_split_to_table('D d a A c b CD Capacitor',' ') as bar
   )
SELECT bar FROM foo
ORDER BY bar collate "en_US.utf8";
    bar    
-----------
 a
 A
 b
 c
 Capacitor
 CD
 d
 D
(8 rows)

那么en_US.UTF-8和en_US.utf8有不同的排序顺序?

So en_US.UTF-8 and en_US.utf8 have different sort orders?

不,这两者都是相同的,只是一个不同的命名约定.

I’m mystified by why the Debian version appears to be case-insensitive and the OS X version is not.

是的,你是对的.这是Mac中的认行为.对于UTF8编码,排序规则不适用于任何BSD-ish操作系统(包括OSX).

以下是证明:

Problems with sort order (UTF8 locales don’t work

正如a_horse_with_no_name所说,Postgres使用操作系统的整理实现.无法在两个操作系统上获得相同的结果.

在你的情况下,你可能(我说可能)这样做:ORDER BY lower(fieldname).

原文地址:https://www.jb51.cc/postgresql/192233.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐