订购点分隔的数字序列(例如版本号) [英] Ordering dot-delimited numeric sequences (e.g., version numbers)

查看:68
本文介绍了订购点分隔的数字序列(例如版本号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我填写了以下数据:

In my database I've column fill this data:

1
1.1
1.1.1
1.1.1.1
1.1.2
1.10
1.11
1.2
1.9

我想对它进行排序,以得到如下结果:

I want to sort it, to get result looks like this:

1
1.1
1.1.1
1.1.1.1
1.1.2
1.2
1.9    
1.10
1.11

我该怎么做?简单使用 ORDER BY会导致错误的结果,因为它是按字典顺序排列。

How can I accomplish this? Simple use "ORDER BY" gives wrong result because it's lexicographic order.

推荐答案

您可以将字符串拆分为数组,然后将其转换到 int [] 并依赖Postgres对数组的自然排序:

You could split the string to an array, cast it to an int[] and rely on Postgres' natural ordering for arrays:

SELECT   mycolumn
FROM     mytable
ORDER BY STRING_TO_ARRAY(mycolumn, '.')::int[] ASC

这篇关于订购点分隔的数字序列(例如版本号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆