PostgreSQL ORDER BY空格 [英] Postgresql ORDER BY spaces

查看:159
本文介绍了PostgreSQL ORDER BY空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有一列的表,即:

Let say I have a table with one column, i.e.:

5 absolute
5.0
5.1
last
50
5 elite
edge

我需要将其订购(使用PostgreSQL方法):

I need to order this to (using postgresql methods):

5 absolute
5 elite
5.0
5.1
50
edge
last

但是如果我使用经典的 ORDER BY column ASC,我将得到:

But if I use classic "ORDER BY column ASC" I get:

50
5.0
5.1
5 absolute
5 elite
edge
last

有有很多子字符串或使用工具,但我不明白它们的工作原理。

There are a lot of tools like substring or using, but I can't understand how they works.

我需要做什么?

推荐答案

不知道,可能是这样的:

Don't know, may be something like this:

with cte as (
   select col1, regexp_split_to_array(col1, ' ') as d
   from Table1

)
select col1
from cte
order by
    d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' desc,
    case
        when d[1] ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$' then
            d[1]::numeric
    end,
    d[2]

sql小提琴演示

该函数将字符串按空格分割为数组,将第一个条目转换为数字,然后按此数字和其余字符串对结果进行排序

this one splits string by spaces to array, convert first entry to numeric and sorts results by this number and remaining string

这篇关于PostgreSQL ORDER BY空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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