Oracle SQL查询中的标识符无效

时间:2021-06-19 06:18:59

I'm kind of a beginner in programming and I can't succeed in running this query on Oracle SQL.

我是编程的初学者,我无法在Oracle SQL上运行此查询。

What I would like to achieve is selecting the id and the names (aya_id, aya_name) from a table AYANTDROIT, and for each of these ids another name which is linked to the id in another table, BENEFICE. The problem is, in my query on the table benefice, I need to get back the id selected in the first line, and no matter how I always end up with an

我想要实现的是从表AYANTDROIT中选择id和名称(aya_id,aya_name),并为这些ID中的每一个添加另一个名称,该名称链接到另一个表BENEFICE中的id。问题是,在我对表格的查询中,我需要找回第一行中选择的id,无论我怎么总是最终得到一个

ORA-00904 : invalid identifier : aya.aya_id

ORA-00904:无效标识符:aya.aya_id

Here is my code:

这是我的代码:

SELECT DISTINCT aya.aya_id, 
aya_name,
(SELECT aya_name
FROM AYANTDROIT aya2 inner join  
     (SELECT ben_aya_id,
      level lev,
      ben_ben_id
      FROM benefice 
      START WITH ben_aya_id = **aya.AYA_ID**
      CONNECT BY prior ben_ben_id = ben_aya_id
      ORDER BY lev desc
      )
 on aya2.aya_id = ben_ben_id
 where rownum = 1),
FROM AYANTDROIT aya
ORDER BY aya_name

However, when I submit this following query, aya.aya_id does not return any error.

但是,当我提交以下查询时,aya.aya_id不会返回任何错误。

SELECT DISTINCT aya.aya_id,
  aya_name,
  (SELECT aya_name 
   FROM AYANTDROIT aya2 
   WHERE aya2.aya_id = 
      (SELECT ben_ben_id 
       FROM benefice LEFT OUTER JOIN ayantdroit ayad 
                      ON ben_aya_id = ayad.aya_id 
                      WHERE ayad.aya_id = **aya.AYA_ID**
      )
   )
FROM AYANTDROIT aya
ORDER BY aya_name

Would anyone know why I can call this aya_id in the second case and not in the first? It would be really helpful here :)

有谁知道为什么我可以在第二种情况下调用这个aya_id而不是第一种情况?这里真的很有帮助:)

Thanks all for your time and have a nice day!

感谢您的所有时间,祝您度过愉快的一天!

1 个解决方案

#1


0  

The problem is that aya.aya_id is too deeply nested in correlated subquery. There are several ways to rebuild your query, here is one:

问题是aya.aya_id在相关子查询中嵌套得太深了。有几种方法可以重建您的查询,这里有一个:

select distinct a1.aya_id, a1.aya_name, a2.aya_name
  from ayantdroit a1
  left join (
    select connect_by_root(ben_aya_id) root, ben_ben_id
      from benefice where connect_by_isleaf = 1
      start with ben_aya_id in (select aya_id from ayantdroit)
      connect by prior ben_ben_id = ben_aya_id) b
  on a1.aya_id = b.root
  left join ayantdroit a2 on a2.aya_id = b.ben_ben_id

Please read also similiar question on Asktom site.

请在Asktom网站上阅读类似的问题。

#1


0  

The problem is that aya.aya_id is too deeply nested in correlated subquery. There are several ways to rebuild your query, here is one:

问题是aya.aya_id在相关子查询中嵌套得太深了。有几种方法可以重建您的查询,这里有一个:

select distinct a1.aya_id, a1.aya_name, a2.aya_name
  from ayantdroit a1
  left join (
    select connect_by_root(ben_aya_id) root, ben_ben_id
      from benefice where connect_by_isleaf = 1
      start with ben_aya_id in (select aya_id from ayantdroit)
      connect by prior ben_ben_id = ben_aya_id) b
  on a1.aya_id = b.root
  left join ayantdroit a2 on a2.aya_id = b.ben_ben_id

Please read also similiar question on Asktom site.

请在Asktom网站上阅读类似的问题。