我如何在oracle中查询多对多?

时间:2013-05-01 09:07:41

标签: sql oracle

我正在尝试使用以下代码创建多对多的子查询。

    SELECT e.ep_suragate_pk  FROM episode e 
    JOIN (
      SELECT n.name FROM actor n 
      JOIN episode_actor ea 
      ON n.act_suragate_pk = ea.act_suragate_pk
      ) ep_act 
    ON ep_act.ep_suragate_pk = e.ep_suragate_pk;

我收到一个ORA-00904:无效的标识符错误,但所有列都存在。

     episode
     -------
     ep_suragate_pk


     episode_actor
     -------------
     ep_suragate_pk
     act_suragate_pk


     actor
     -----
     act_suragate_pk

如果有人能帮我理解我哪里出错了,我们将不胜感激。感谢。

4 个答案:

答案 0 :(得分:3)

子查询需要选择列ea.ep_suragate_pk才能解决无效标识符问题。尚不确定是否会获得您想要的结果。

答案 1 :(得分:2)

内部查询

   (
  SELECT n.name FROM actor n 
  JOIN episode_actor ea 
  ON n.act_suragate_pk = ea.act_suragate_pk
  ) ep_act 

没有列p_suragate_pk用于加入

  ep_act 
ON ep_act.ep_suragate_pk = e.ep_suragate_pk;

所以你可以像这样修改

SELECT e.ep_suragate_pk  FROM episode e 
JOIN (
  SELECT n.name,n.act_suragate_pk FROM actor n 
  JOIN episode_actor ea 
  ON n.act_suragate_pk = ea.act_suragate_pk
  ) ep_act 
ON ep_act.ep_suragate_pk = e.ep_suragate_pk;

答案 2 :(得分:2)

您不需要所有这些子查询

select  e.ep_suragate_pk  
from    episode e  , episode_actor ea , actor n
where   ea.ref_id = e.ep_suragate_pk
and     n.act_suragate_pk = ea.act_suragate_pk

答案 3 :(得分:1)

应选择用于加入的列。所以试试:

SELECT e.ep_suragate_pk  FROM episode e 
JOIN (
  SELECT n.name, ref_id FROM actor n 
  JOIN episode_actor ea 
  ON n.act_suragate_pk = ea.act_suragate_pk
  ) ep_act 
ON ep_act.ref_id = e.ep_suragate_pk;