Итак, вы в основном решили свою проблему.Вы можете перечислить идентификаторы, как вы сделали, или вы можете хранить их где-то и использовать в IN
подзапрос, как здесь, например:
with tblCategory(CategoryId, Name, Parent) as (
select 1, 'Clothing', null from dual union all
select 2, 'Men''s Wear', 1 from dual union all
select 3, 'Shirt', 2 from dual union all
select 4, 'T-Shirt', 3 from dual union all
select 5, 'Women''s Wear', 1 from dual union all
select 6, 'Salwar', 5 from dual union all
select 7, 'Saree', 5 from dual union all
select 8, 'Electronics', null from dual union all
select 9, 'Computers', 8 from dual union all
select 10, 'Mobiles', 8 from dual ),
ids(cid) as (select 3 from dual union all select 6 from dual)
select distinct categoryid, name, parent
from tblcategory
start with categoryid in (select cid from ids)
connect by categoryid = prior parent
Результат:
CATEGORYID NAME PARENT
---------- ------------ ----------
6 Salwar 5
3 Shirt 2
5 Women's Wear 1
2 Men's Wear 1
1 Clothing
Вы также можете произвестиболее читаемый вывод, как здесь:
select connect_by_root(categoryid) root,
sys_connect_by_path(name, ' => ') path
from tblcategory
where connect_by_isleaf = 1
start with categoryid in (select cid from ids)
connect by categoryid = prior parent
Результат:
ROOT PATH
------ --------------------------------------------------------------------------------
3 => Shirt => Men's Wear => Clothing
6 => Salwar => Women's Wear => Clothing