我有一个oracle表包含和ID列,ID_child列和几个事实字段。我知道一个ID,我想找到它的顶级父母。我知道的id会放入ID_child字段,ID就是它的父节点。 这个查询有效,但我只得到一行,我可以用常规的SQL查询。 如何获取热门ID?
SELECT
item_tbl.id, item_tbl.id_child
FROM item_tbl
START WITH item_tbl.id_child = 12510646
CONNECT BY PRIOR item_tbl.id_child = item_tbl.id
我得到的结果:
ID ID_CHILD
12512162 12510646
答案 0 :(得分:0)
例如: 事物的连接顺序;父母需要在左侧。
With item_tbl (ID, ID_CHILD) as
(SELECT 1,2 from dual UNION ALL
SELECT 2,3 from dual UNION ALL
SELECT 3,4 from dual)
SELECT
item_tbl.id, item_tbl.id_child, level
FROM item_tbl
START WITH item_tbl.id_child = 4
CONNECT BY PRIOR item_tbl.id= item_tbl.id_child
Order by level desc;
答案 1 :(得分:0)
如何获得最高ID?
您可以使用CONNECT_BY_ISLEAF
伪列:
SELECT id, id_child
FROM item_tbl
WHERE CONNECT_BY_ISLEAF = 1
START WITH id_child = 12510646
CONNECT BY PRIOR id = id_child
如果这不起作用,请尝试交换CONNECT BY
子句中的术语。