Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- '{30,32,34,20,19,18,17,16,15,14}'
- select *
- from teste1_elements
- where category_id = any(fn_category_get_childs_v2(12))
- WITH RECURSIVE children AS (
- SELECT category_id
- FROM category
- WHERE parent_id = id_pai
- UNION ALL
- SELECT c.category_id
- FROM children ch
- JOIN category c ON c.parent_id = ch.category_id
- )
- SELECT *
- FROM children;
- CREATE OR REPLACE FUNCTION f_cat_children(_id_pai int, OUT category_id int)
- RETURNS SETOF int LANGUAGE SQL AS
- $func$
- WITH RECURSIVE children AS (
- SELECT c.category_id
- FROM category c
- WHERE c.parent_id = $1
- UNION ALL
- SELECT c.category_id
- FROM children ch
- JOIN category c ON c.parent_id = ch.category_id
- )
- SELECT *
- FROM children;
- $func$;
- SELECT t.*
- FROM teste1_elements t
- JOIN f_cat_children(12) USING (category_id)
- WITH RECURSIVE children AS (
- SELECT category_id
- FROM category
- WHERE parent_id = id_pai
- UNION ALL
- SELECT c.category_id
- FROM children ch
- JOIN category c ON c.parent_id = ch.category_id
- )
- SELECT t.*
- FROM children
- JOIN teste1_elements t USING (category_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement