我想使用过滤查询来检查一个数组是否等于另一个多维数组的元素,该数组可以被视为数组的数组。
例如:
给定多维数组 {{1,2}, {3,4}, {5,6}}
我想检查数组元素之一是否为一维数组。
预期结果:
- 输入:
{1,2}
或{3,4}
-> 输出:TRUE
- 输入:
{2,3}
或{1,5}
-> 输出:FALSE
我已经尝试过<@
,但它返回 TRUE
对于所有示例情况,我不能使用 ANY
无需对多维数组进行切片。
有没有人有不使用pgplsql
的解决方案?
最佳答案
如果没有任何 pgpsql,这似乎是一个很难解决的问题。不过,如果利用这个函数,那就简单多了: https://wiki.postgresql.org/wiki/Unnest_multidimensional_array
CREATE OR REPLACE FUNCTION public.reduce_dim(anyarray)
RETURNS SETOF anyarray AS
$function$
DECLARE
s $1%TYPE;
BEGIN
FOREACH s SLICE 1 IN ARRAY $1 LOOP
RETURN NEXT s;
END LOOP;
RETURN;
END;
$function$
LANGUAGE plpgsql IMMUTABLE;
使用:
create table array_test (arr integer[][]);
insert into array_test (select '{{1,2}, {3,4}, {5,6}}');
select (case when '{1,2}' in (select reduce_dim(arr) from array_test) then true
else false end);
case
------
t
(1 row)
select (case when '{1,4}' in (select reduce_dim(arr) from array_test) then true
else false end);
case
------
f
(1 row)
关于arrays - 如何检查数组是否为多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45390374/