实际上,您可以像 where "api_version > 'vX.X.XX" 那样进行比较——好吧。函数 regexp_split_to_array 可用于将版本号转换为整数数组(需要丢失 "v" )。 Postgres 可以使用常规比较运算符比较整数数组。
-- fails to sort version properly and gets version incorrect
with versions (name, version) as
( values ('first', 'v0.0.1')
, ('second', 'v1.0.1')
, ('third', 'v1.2.1')
, ('forth','v1.10.1')
)
select name, substring(version,2) as version
, version > 'v1.3.5' "> v1.3.5"
from versions
order by 2;
-- properly sorts version and properly identifies the version order
with versions (name, version) as
( values ('first', 'v0.0.1')
, ('second', 'v1.0.1')
, ('third', 'v1.2.1')
, ('forth','v1.10.1')
)
select name, regexp_split_to_array(ver, '(\.)')::int[] as version
, regexp_split_to_array(ver, '(\.)')::int[] > regexp_split_to_array('1.3.5', '(\.)')::int[] "> v1.3.5"
from (select name, substring(version,2) as ver
from versions
) v
order by 2;
现在进行实际版本比较:
with versions (name, version) as
( values ('first', 'v0.0.1')
, ('second', 'v1.0.1')
, ('third', 'v1.2.1')
, ('forth','v1.10.1')
)
, target (version) as
( values ('v1.10.0') )
select name, version
from ( select name, regexp_split_to_array(ver, '(\.)')::int[] as version
from (select name, substring(version,2) as ver
from versions
) v
) v2
where version > (select regexp_split_to_array( (substring(version,2))::text , '(\.)')::int[] from target) ;
上面的复杂性来自必须处理版本号中的“v”。没有它,这将简化为:
with versions (name, version) as
( values ('first', '0.0.1')
, ('second', '1.0.1')
, ('third', '1.2.1')
, ('forth','1.10.1')
)
, target as
( select regexp_split_to_array( '1.10.0', '(\.)')::int[] as version)
select v.name, v.version
from (select name, regexp_split_to_array(version, '(\.)')::int[] as version from versions) v
where v.version > (select t.version from target t) ;