问题描述
SQL查询对文本数组进行排序
iotedge logs edgeAgent
GO代码对字符串数组https://play.golang.org/p/UsWAKTz2Zj4进行排序
SELECT unnest(ARRAY['Go','[Bravo]','Gopher','[Alpha]','Grin','Delta']) ORDER BY 1 ASC;
unnest
---------
[Alpha]
[Bravo]
Delta
Go
Gopher
Grin
为什么结果不同?
解决方法
在SQL查询中,您unnest
数组。这似乎给出了排序顺序,而没有或忽略[
和]
。
在执行过程中,方括号被视为字符串的一部分。这两个函数似乎按字典顺序排序。
,Go似乎使用“ ascii”排序。
您可以在Postgres中使用collate "C"
来实现:
SELECT word
from unnest(ARRAY['Go','[Bravo]','Gopher','[Alpha]','Grin','Delta']) as t(word)
ORDER BY word collate "C" ;