如何在PostgreSQL中对包含星号的VARCHAR列进行排序

时间:2018-12-10 19:23:26

标签: sql postgresql sql-order-by

select folder_name  -- varchar
from ags_secured_endpoint_definition;

返回:

Utilities
Utilities
UtilityServices
*** ROOT ***
Midstream
EP
EP
EP
EP
EP
EP
Midstream
Culture
Culture
Culture
Culture
Culture
Culture
CustomUtilities
Basin
Basin
EP
EP
EP

对不起,我得到了意外的结果(“ *根* ”):

select folder_name 
from ags_secured_endpoint_definition
ORDER BY folder_name;

返回:

Basin
Basin
Culture
Culture
Culture
Culture
Culture
Culture
CustomUtilities
EP
EP
EP
EP
EP
EP
EP
EP
EP
Midstream
Midstream
*** ROOT ***
Utilities
Utilities
UtilityServices

1 个答案:

答案 0 :(得分:2)

这取决于您的排序规则,可以通过

看到
SHOW lc_collate;

很显然,排序时排序规则会忽略空格和星号,因此*** ROOT ***的排序方式类似于ROOT

如果您不喜欢这样的排序规则,则可以选择其他排序规则,例如二进制:

SELECT folder_name 
FROM ags_secured_endpoint_definition
ORDER BY folder_name COLLATE "C";