【问题标题】:Finding first array value that contains a string in PostgreSql在 PostgreSql 中查找包含字符串的第一个数组值
【发布时间】:2021-08-20 21:02:57
【问题描述】:
SELECT array_position(ARRAY, 'fillfactor=%') as position
FROM table;
我希望能够使用与上面类似的代码。我不知道填充因子是什么,所以我无法进行精确的字符串匹配,而是想获取以该语句开头的第一个值。请注意,此示例假定 ARRAY 是 text[] 列。有人可以帮忙吗?
【问题讨论】:
标签:
sql
postgresql
pgadmin
postgresql-9.5
【解决方案1】:
没有像 array_regex_position 或类似的内置函数可以做到这一点。您将需要取消嵌套数组并在结果元素上使用模式匹配运算符或函数。假设您正在查询 pg_class.reloptions,这应该会给您大致的想法(这是在 10.5 上,我现在没有任何 9.X 服务器在运行):
testdb=# create table t_with_fillfactor(asdf text) with (fillfactor=50);
CREATE TABLE
testdb=# select relname,
substr(reloption, length('fillfactor=')+1)::integer as fill_factor
from pg_class, unnest(reloptions) as reloption
where relname='t_with_fillfactor'
and reloption ~ 'fillfactor=.*';
relname | fill_factor
-------------------+-------------
t_with_fillfactor | 50
(1 row)