首页 > 解决方案 > oracle sql 内联查询标识符无效

问题描述

我有以下 oracle sql 查询

select tren,origen,area
from (
select cod_serv tren,a.origen origen
from r_incidencias r,
(select tren,origen
from r_trenes80_e
union
select tren,origen
from k_trenes80) a 
,
(select id_area_productiva area,cod_estacion estacion
from mer_est_area_prod meap
where a.tren = r.cod_serv
and meap.cod_estacion = a.origen
 ) f)

当我执行这句话时,我得到了错误

a.origen 标识符无效。

我希望表 mer_est_area_prod 中的字段 origen 与查询 a 中的字段 origen 相等。

在表 mer_est_area_prod 中存在字段 origen

标签: sqloracle

解决方案


不能在子查询join中直接使用joined table column,可以移到子查询外的where条件

前任:

SELECT tren,origen,area
FROM 
(
    SELECT cod_serv tren ,a.origen origen
    FROM r_incidencias r,
    (
        SELECT tren,origen
        FROM r_trenes80_e
        UNION
        SELECT tren,origen
        FROM k_trenes80
    ) a,
    (
        SELECT id_area_productiva area,cod_estacion estacion
        FROM mer_est_area_prod meap
    ) f
    WHERE a.tren = r.cod_serv
    AND f.estacion = a.origen
)

最好使用新的 ANSI sql 连接条件,因为您尝试连接的内容很清楚

前任:

SELECT cod_serv tren ,a.origen origen, meap.id_area_productiva area
FROM r_incidencias r
INNER JOIN 
(
        SELECT tren,origen
        FROM r_trenes80_e
        UNION
        SELECT tren,origen
        FROM k_trenes80
) a ON a.tren = r.cod_serv
INNER JOIN mer_est_area_prod meap ON meap.cod_estacion = a.origen

推荐阅读