ORA-00904:在from子句中使用自然连接并与子查询中的内部连接结合使用时,标识符无效

时间:2015-01-08 12:19:28

标签: sql oracle11g

我想在main select语句中使用city列的前缀(即:l.city),就像在内部select子查询语句中一样,以获得更“理解”和更好看的方式可以这么说但是我不能,因为它给了我ORA-00904:“L”。“城市”:标识符无效,仅使用没有标识符的城市,为什么? 这是代码:

   SELECT d.department_name, l.city
   FROM  departments d
   NATURAL JOIN (SELECT l.location_id,l.city,l.country_id
                  FROM       locations l
                  INNER JOIN countries c
                  ON (l.country_id = c.country_id)
                  INNER JOIN regions r
                  ON (c.region_id = r.region_id)
                  WHERE r.region_name = 'Europe');

3 个答案:

答案 0 :(得分:2)

您的"L"别名位于视图中,因此在您要使用它的位置不可见。

试试这个:

   SELECT d.department_name, x.city
   FROM  departments d
   NATURAL JOIN (SELECT l.location_id,l.city,l.country_id
                  FROM       locations l // <-- "l" has no scope outside brackets
                  INNER JOIN countries c
                  ON (l.country_id = c.country_id)
                  INNER JOIN regions r
                  ON (c.region_id = r.region_id)
                  WHERE r.region_name = 'Europe') x;

答案 1 :(得分:2)

只需为子查询添加别名,或删除l.中的l.city

SELECT d.department_name, l.city
FROM  departments d NATURAL JOIN
      (SELECT l.location_id,l.city,l.country_id
       FROM locations l INNER JOIN
            countries c
            ON (l.country_id = c.country_id) INNER JOIN
            regions r
            ON (c.region_id = r.region_id)
       WHERE r.region_name = 'Europe'
      ) l

答案 2 :(得分:1)

你需要定义&#39; l&#39;主查询中的别名。

SELECT d.department_name, subl.city
FROM  departments d
NATURAL JOIN (SELECT l.location_id,l.city,l.country_id
              FROM locations l
              INNER JOIN countries c ON l.country_id = c.country_id
              INNER JOIN regions r ON c.region_id = r.region_id
              WHERE r.region_name = 'Europe') subl;