Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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
Sql 无法在分层查询中引用内部查询中的列?_Sql_Oracle_Oracle11g_Oracle10g_Hierarchical Query - Fatal编程技术网

Sql 无法在分层查询中引用内部查询中的列?

Sql 无法在分层查询中引用内部查询中的列?,sql,oracle,oracle11g,oracle10g,hierarchical-query,Sql,Oracle,Oracle11g,Oracle10g,Hierarchical Query,我正在使用分层查询从表结构中提取详细信息。我正在“开始于”条件下从内部查询中引用列,但其抛出未知列错误,请检查 查询: 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

我正在使用分层查询从表结构中提取详细信息。我正在“开始于”条件下从内部查询中引用列,但其抛出未知列错误,请检查

查询:

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
问题在于突出显示的区域,我计划从内部查询中引用该列


i、 e.从object\u id=sopi开始。object\u id不工作,请帮助

我认为您必须将
sopi
子查询移动到
子句开始,并在条件下使用

样本测试数据:

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

我认为您必须将
sopi
子查询移动到
中,从
子句开始,并在
条件中使用

样本测试数据:

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

问题是您试图引用孙子子查询中祖父母查询中的列。如果列位于父查询中,则只能从子查询外部引用该列,如下所示:

当嵌套子查询引用表中的列时,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

问题是您试图引用孙子子查询中祖父母查询中的列。如果列位于父查询中,则只能从子查询外部引用该列,如下所示:

当嵌套子查询引用表中的列时,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

@rohansr002如果sopi子查询中的ID在temp_对象表中没有第4级行,则Pounder的查询返回的结果与原始查询不一样-您必须进行外部联接才能获得“缺失”行。如果总是保证temp_对象子查询总是返回一行,那么这个查询就会工作fine@rohansr002如果sopi子查询中的ID在temp_对象表中没有第4级行,则Pounder的查询返回的结果与原始查询不一样-您必须进行外部联接才能获得“缺少的”排。如果总是保证temp_对象子查询总是返回一行,那么这个查询就可以正常工作