是否有postgresql函数,最好是本地函数,可以将字符串(如“banana”)分类为“aaabnn”?Postgresql函数对字符串中的字符进行排序
排序的算法效率并不重要,因为单词永远不会太长。但是,数据库联接效率有一些但并不重要。
是否有postgresql函数,最好是本地函数,可以将字符串(如“banana”)分类为“aaabnn”?Postgresql函数对字符串中的字符进行排序
排序的算法效率并不重要,因为单词永远不会太长。但是,数据库联接效率有一些但并不重要。
有这样的功能,没有本地的功能,但你可以使用regexp_split_to_table
,因为这可以这样做:
select theword
from (select regexp_split_to_table('banana',E'(?=.)') theword) tab
order by theword;
结果将是:
theword
a
a
a
b
n
n
这(?=.)
将每个字符离开分裂字符作为分隔符。它也将识别空间。如果你对空格有一个词并且不想要它(空格),则使用E'(\\s*)'
匹配任何空格字符。我不记得E
的含义。我会尽快搜索和编辑答案。
正如DOCs一节“regexp_split_to_table”
编辑解释说:正如我说过:E
意义的字符串之前,你可以在这里看到:What's the "E" before a Postgres string?
嗯......我的灵感来自于这样做:'select array_to_string(array(select unnest(string_to_array('banana', null))order by 1),'')as r;'。 'string_to_array'与'unnest'的组合更加正交,'array'和'array_to_string'锦上添花,将返回的字符串自身排序。 – user2297550
虽然它的工作,我怀疑你的解决方案将更具性能,那么我提供的。然后比较两者的执行计划。 –
'选择array_to_string(阵列(选择UNNEST (string_to_array('banana',null))order by 1),'')as r;' – user2297550