1

以下のテストデータの階層データが必要です。ユーザー「jones」の場合、以下のレコードをフェッチする必要があります。CONNECTBY句とSTARTWITH句に問題があります。また、menu_itemsテーブルとrole_menu_itemsテーブルの間にいくつかのJOINの問題があると思います。

ユーザーとそれに対応する役割については、以下のデータに影響を与える必要があります

select  mi.id,  mi.NAME,mi.MEN_ID,mi.ICON,mi.ACTION--,r.NAME 
from
menu_items mi,
role_menu_items rmi,
roles r,
USERS U,
USER_ROLES UR
where
mi.ID=rmi.MIT_ID and
rmi.ROL_ID=r.id and
r.id=UR.ROLE_ID and
U.ID=UR.USER_ID and
U.NAME='jones'
start with mi.men_id is null
connect by   prior  mi.ID=mi.men_id

出力

1   Menu1           Menu1       /images/home.gif
3   Help            Help    
4   page1   a   1   page1   1   /images/email.gif
7   about   d   1   about   3

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (1,'Menu1',null,null,'Menu1',null,'/images/home.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (2,'Menu2',null,null,'Menu2',null,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (3,'Help',null,null,'Help',null,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (4,'page1','a',1,'page1',1,'/images/email.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (5,'page2','b',2,'page2',1,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (6,'page3','c',1,'page3',2,'/images/photo.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (7,'about','d',1,'about',3,null);

insert into ROLES (ID, NAME)
values (1, 'administrator');

insert into ROLES (ID, NAME)
values (2, 'user');

Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,4);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,5);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,6);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,7);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (2,4);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (2,7);

Insert into USER_ROLES (USER_ID,ROLE_ID) values (2,2);
Insert into USER_ROLES (USER_ID,ROLE_ID) values (1,1);
Insert into USER_ROLES (USER_ID,ROLE_ID) values (3,2);

Insert into USERS (ID,NAME,PASSWORD) values (2,'scott','scott');
Insert into USERS (ID,NAME,PASSWORD) values (1,'john','john');
Insert into USERS (ID,NAME,PASSWORD) values (3,'jones','jones');
4

1 に答える 1

2

悪いスキームですが、可能です

1.ユーザー ロールのすべての menu_id を選択します


select RMI.MIT_ID
from ROLE_MENU_ITEMS RMI
   , ROLES R
   , USERS U
   , USER_ROLES UR
where RMI.ROL_ID = R.ID
  and R.ID = UR.ROLE_ID
  and U.ID = UR.USER_ID
  and U.NAME = 'jones'

2.逆階層クエリ


select MI.*
     , max(LEVEL) over () + 1 - level as rev_level
     , lpad(' ', (max(LEVEL) over () + 1 - LEVEL) * 3, ' ') || MI.NAME as LEVEL_NAME
     , rownum RN
from MENU_ITEMS MI
connect by prior MEN_ID = ID
start with ID IN (select RMI.MIT_ID
                  from ROLE_MENU_ITEMS RMI
                     , ROLES R
                     , USERS U
                     , USER_ROLES UR
                  where RMI.ROL_ID = R.ID
                    and R.ID = UR.ROLE_ID
                    and U.ID = UR.USER_ID
                    and U.NAME = 'jones')

3.reverse(逆階層クエリ)


select ID
     , NAME
     , SHORTCUT
     , DISPLAY_SEQUENCE
     , ACTION
     , MEN_ID
     , ICON
     , LEVEL_NAME
from 
(
    select MI.*
         , max(LEVEL) over () + 1 - level as rev_level
         , lpad(' ', (max(LEVEL) over () + 1 - LEVEL) * 3, ' ') || MI.NAME as LEVEL_NAME
         , rownum RN
    from MENU_ITEMS MI
    connect by prior MEN_ID = ID
    start with ID IN (select RMI.MIT_ID
                      from ROLE_MENU_ITEMS RMI
                         , ROLES R
                         , USERS U
                         , USER_ROLES UR
                      where RMI.ROL_ID = R.ID
                        and R.ID = UR.ROLE_ID
                        and U.ID = UR.USER_ID
                        and U.NAME = 'jones')
)
group by ID --distinct with order
       , name
       , SHORTCUT
       , DISPLAY_SEQUENCE
       , ACTION
       , MEN_ID
       , ICON
       , LEVEL_NAME
order by min(-RN)

サブクエリを使用して、ルート項目のソートを ID で保持することが可能

select ID
     , NAME
     , SHORTCUT
     , DISPLAY_SEQUENCE
     , ACTION
     , MEN_ID
     , ICON
     , LEVEL_NAME
from 
(
    select ID
         , NAME
         , SHORTCUT
         , DISPLAY_SEQUENCE
         , ACTION
         , MEN_ID
         , ICON
         , MAX(LVL) OVER () + 1 - LVL
         , rpad(' ', (max(LVL) over () + 1 - LVL) * 3, ' ') || NAME as LEVEL_NAME
         , RN
    from (
        select MI.*
             , rownum RN
             , level LVL
        from MENU_ITEMS MI
        connect by prior MEN_ID = ID
        start with ID IN (select RMI.MIT_ID
                          from ROLE_MENU_ITEMS RMI
                             , ROLES R
                             , USERS U
                             , USER_ROLES UR
                          where RMI.ROL_ID = R.ID
                            and R.ID = UR.ROLE_ID
                            and U.ID = UR.USER_ID
                            and U.NAME = 'jones')
        order siblings by -ID
    )
)
group by ID --distinct with order
       , name
       , SHORTCUT
       , DISPLAY_SEQUENCE
       , ACTION
       , MEN_ID
       , ICON
       , LEVEL_NAME
order by min(-RN)
于 2012-05-22T05:15:42.350 に答える