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

ORA-00904: invalid identifier when using natural join in the from clause combined with inner joins in subquery

我也想在主 select 语句中为城市列使用前缀(即:l.city),就像在内部 select 子查询语句中一样"understanding" 可以说是一种更好看的方式,但我不能,因为它给了我 ORA-00904:"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" 别名位于视图内,因此在您要使用它的地方不可见。

试试这个:

   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;

只需给您的子查询一个别名或删除 l.city 中的 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'
      ) l

您需要在主查询中定义 'l' 别名。

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;