0

我正在使用分层查询从我的表结构中获取详细信息。我在'START WITH'条件下从内部查询中引用列,但它抛出未知列错误请检查

询问 :

select
  (select obj.name
    from (select LEVEL parentLevel, object_id id, name from temp_object START WITH object_id=sopi.OBJECT_ID CONNECT BY PRIOR parent_id = object_id) obj
            where parentLevel=4) "temp Order Name"
 from
  (SELECT OBJECT_ID
   FROM temp_params
   WHERE value = 'Add' AND object_id IN
                           (SELECT object_id
                            FROM temp_references
                            WHERE reference IN
                                  (SELECT object_id
                                   FROM temp_params
                                   WHERE list_id = 9133409) AND attt_id = '9133410')) sopi

问题在于突出显示的区域,我计划从内部查询中引用列。

START WITH object_id=sopi.OBJECT_ID 不起作用,请帮助

4

2 回答 2

2

问题是您试图在孙子查询中引用祖父查询中的列。如果它在父查询中,则只能从子查询外部引用列,根据文档

当嵌套子查询引用表中的列时,Oracle 执行相关子查询,该表引用了子查询上一级的父语句。

幸运的是,您不需要引入的额外子查询级别;你应该能够做到:

select (select     name
        from       temp_object
        where      level = 4
        start with object_id = sopi.object_id
        connect by prior parent_id = object_id) obj "temp Order Name"
from   (select object_id
        from   temp_params
        where  value = 'Add'
        and    object_id in (select object_id
                             from   temp_references
                             where  reference in (select object_id
                                                  from   temp_params
                                                  where  list_id = 9133409)
                             and    attt_id = '9133410')) sopi
于 2016-05-12T12:13:52.127 回答
0

我认为您必须将sopi子查询移入start with子句并使用in条件。

SQLFiddle 演示

select name
  from (select level parentlevel, object_id id, name
          from temp_object
         start with object_id in
                    (select object_id
                       from temp_params
                      where value = 'Add'
                        and object_id in
                            (select object_id
                               from temp_references
                              where reference in
                                    (select object_id
                                       from temp_params
                                      where list_id = 9133409)
                                and attt_id = '9133410'))
        connect by prior parent_id = object_id)
 where parentlevel = 4

样本测试数据:

create table temp_params (object_id number(3), list_id number(8), value varchar2(3));
insert into temp_params values (1, 9133409, 'Add');
insert into temp_params values (2, 9133411, 'Add');
insert into temp_params values (3, 9133412, 'Add');

create table temp_references (object_id number(3), reference number(3), attt_id varchar2(8));
insert into temp_references values (2, 1, '9133410');
insert into temp_references values (3, 1, '9133410');

create table temp_object (object_id number(8), parent_id number(8), name varchar2(10));
insert into temp_object values ( 2, 21, 'Object 02');
insert into temp_object values (21, 22, 'Object 21');
insert into temp_object values (22, 23, 'Object 22');
insert into temp_object values (23, 24, 'Object 23');
insert into temp_object values (24, 25, 'Object 24');
insert into temp_object values ( 3, 31, 'Object 03');
insert into temp_object values (31, 32, 'Object 31');
insert into temp_object values (32, 33, 'Object 32');
insert into temp_object values (33, 34, 'Object 33');

输出:

NAME
----------
Object 23
Object 33
于 2016-05-12T11:55:02.747 回答