join - query recursive and others tables in postgresql? -
i have little problem make recursive query , can’t extract data need.
i have 3 tables (menu, type_role, privilege)
menu ---------------------------------------------------------- | id| parent_id | tittle | controller | action | | 1 | | users | | | | 2 | 1 | create user | users | create | | 3 | 1 | edit user | users | edit | | 4 | 1 | show users | users | show | | 5 | | contacts | | | | 6 | 5 | create contacs | contacts | create | | 7 | 5 | edit contacts | contacts | edit | | 8 | | inventory | | | | 9 | 8 | register piece | pieces | register | | 10| 8 | show pieces | pieces | show |
type_role --------------------- | id| role | | 1 | administrator | | 2 | technical | | 3 | operator |
privilege --------------------- | role_id | menu_id | | 1 | 2 | | 1 | 3 | | 1 | 4 | | 1 | 6 | | 1 | 7 | | 1 | 9 | | 1 | 10 | | 2 | 9 | | 2 | 10 | | 3 | 6 | | 3 | 7 | | 3 | 10 |
i need extract:
id, parent_id, controller, action id of type of role equals 1
i made query , data, query extract parents not related type of user , don't need this.
i want extract menu items , parent user type
select menu.id,menu.parent_id,menu.controller,menu.action privilege inner join menu on menu.id = privilege.menu_id inner join type_role on type_role.id = privilege.role_id type_role.id = 1 union select menu_recur.id,menu_recur.parent_id,menu_recur.menu_recur,menu_recur.action menu menu_recur inner join menu menutwo on menu_recur.id = menutwo.parent_id
i think should use recursive have not worked kind of recursive queries
here's recursive query loops parent entries.
with recursive list ( select menu.id, parent_id, tittle, controller, action menu join privilege on menu.id = privilege.menu_id join type_role on type_role.id = privilege.role_id type_role.role = 'technical' union select menu.id, menu.parent_id, menu.tittle, menu.controller, menu.action list join menu on menu.id = list.parent_id ) select distinct * list order id ;
this prints:
id | parent_id | tittle | controller | action ----+-----------+----------------+------------+---------- 8 | | inventory | | 9 | 8 | register piece | pieces | register 10 | 8 | show pieces | pieces | show (3 rows)
see working @ sql fiddle. example technical
, administrator
includes every row.
Comments
Post a Comment