4

我有一个使用 da_DK.utf8 语言环境创建的 PostgreSQL 8.4 数据库。

dbname=> show lc_collate;
 lc_collate
------------
 da_DK.utf8
(1 row)

当我从表中选择一些东西时,我在字符变化列上排序时,我得到了一个奇怪的行为 IMO。在排序结果时,PostgreSQL 会忽略值前缀的破折号,例如:

 select name from mytable order by name asc;

可能会返回类似的东西

 name
 ----------------
 Ad...
 Ae...
 Ag...
 - Ak....
 At....

破折号前缀似乎被忽略了。

我可以通过在订购时将列转换为 latin1 来解决此问题:

 select name from mytable order by convert_to(name, 'latin1') asc;

我得到了预期的结果:

 name
 ----------------
 - Ak....
 Ad...
 Ae...
 Ag...
 At....

为什么默认情况下会忽略破折号前缀?这种行为可以改变吗?

4

3 回答 3

5
于 2011-02-10T11:00:05.757 回答
1

A workaround that will work in my specific case is to replace dashes with exclamation points. I happen to know that I will never get exclamation points and it will be sorted before any letters or digits.

select name from mytable order by translate(name, '-', '!') asc

It will certainly affect performance so I may look into creating a special column for sorting but I really don't like that either...

于 2011-02-10T12:10:58.287 回答
0

I don't know how seems ordering rules for Dutch, but for Polish special characters like space, dashes etc are not "counted" in sorting in most dictionaries. Some good sort routines do the same and ignores such special characters. Probably in Dutch there is similar rule, and this rule is implemented by Ubuntu locale aware sort function.

于 2011-02-10T11:03:46.687 回答