我有这个函数可以返回树节点的所有子节点:
CREATE OR REPLACE FUNCTION fn_category_get_childs_v2(id_pai integer)
RETURNS integer[] AS
$BODY$
DECLARE
ids_filhos integer array;
BEGIN
SELECT array (
SELECT category_id FROM category WHERE category_id IN (
(WITH RECURSIVE parent AS
(
SELECT category_id , parent_id from category WHERE category_id = id_pai
UNION ALL
SELECT t.category_id , t.parent_id FROM parent
INNER JOIN category t ON parent.category_id = t.parent_id
)
SELECT category_id FROM parent
WHERE category_id <> id_pai
) )
) into ids_filhos;
return ids_filhos;
END;
我想在这样的选择语句中使用它:
select *
from teste1_elements
where category_id in (select * from fn_category_get_childs_v2(12))
我也尝试过这种方式,结果相同:
select *
from teste1_elements
where category_id=any(select * from fn_category_get_childs_v2(12)))
但我收到以下错误:
ERROR: operator does not exist: integer = integer[]
LINE 1: select * from teste1_elements where category_id in (select *...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
该函数返回一个整数数组,是这个问题吗?
SELECT * from fn_category_get_childs_v2(12)
检索以下数组 ( integer[]
):
'{30,32,34,20,19,18,17,16,15,14}'