Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 联接内部的分层查询_Oracle_Hierarchical - Fatal编程技术网

Oracle 联接内部的分层查询

Oracle 联接内部的分层查询,oracle,hierarchical,Oracle,Hierarchical,我正面临一个恼人的问题,我想得到一些帮助 情况就是这样 CREATE TABLE tree_hierarchy ( id NUMBER (20) ,parent_id NUMBER (20) ); CREATE TABLE tree_information ( id NUMBER (20) ,some_text VARCHAR(20) ,tree_id NUMBER (20) ); INSERT INTO tree_hierarchy (id, pare

我正面临一个恼人的问题,我想得到一些帮助

情况就是这样

CREATE TABLE tree_hierarchy (
  id        NUMBER (20)
 ,parent_id NUMBER (20)
);
CREATE TABLE tree_information (
  id        NUMBER (20)
 ,some_text VARCHAR(20)
 ,tree_id NUMBER (20)
);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (2, null);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (4, 2);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (9, 4);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (20, null);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (40, 20);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (90, 40);
INSERT INTO tree_information (id, some_text, tree_id) VALUES (10,'Some teste', 2);
INSERT INTO tree_information (id, some_text, tree_id) VALUES (11,'Other tree', 20);
我想做这样的事情

SELECT hier.*
  FROM tree_information Ti
  JOIN (
        SELECT 
            id,
            parent_id
         FROM tree_hierarchy th
         where connect_by_isleaf = 1
        START WITH th.id = ti.tree_id
        CONNECT BY PRIOR th.id = th.parent_id


  ) hier on 1=1;
但是ti.tree_id在select中不可见

如果我改变起始条件

 START WITH th.parent_id is null
这将永远是错误的。
有人知道如何解决这种情况吗?

如果您能明确提供预期结果,我将不胜感激。 我最好的猜测是:

SELECT hier.*, ti.*
  FROM tree_information Ti
  JOIN (
        SELECT 
            id,
            parent_id,
            connect_by_root th.id as tree_id
         FROM tree_hierarchy th
         where connect_by_isleaf = 1
        START WITH th.id in ( select tii.tree_id from tree_information Tii)
        CONNECT BY PRIOR th.id = th.parent_id
  ) hier on ti.tree_id = hier.tree_id;

ID  PARENT_ID   TREE_ID ID  SOME_TEXT   TREE_ID
 9          4         2 10  Some teste        2
90         40        20 11  Other tree       20

你能告诉我你期望的输出应该是什么吗?“这是我想做的”,然后你会显示代码,但它并没有做你想做的事情(这就是为什么你要首先发布这个)。那我们怎么才能猜出你想做什么呢?从不起作用的代码,而不是别的?请看上面老程序员写的。