Ordering dot-delimited numeric sequences (e.g., version numbers)

前端 未结 2 566
自闭症患者
自闭症患者 2021-01-19 16:22

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:

2条回答
  •  悲哀的现实
    2021-01-19 17:12

    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
    

提交回复
热议问题