在Oracle的Where子句子查询中使用别名或表名,

时间:2015-12-10 11:17:23

标签: sql oracle oracle11g subquery

我需要在查询中显示其他表中的一些字段,我收到了这个错误:

ORA-00904:“THIS _”。“ID”:识别者inválido 00904. 00000 - “%s:无效标识符”

这是查询

SELECT this_."ID" as ID1_47_2_
FROM "ENSAIO_AMOSTRA" this_
inner join "ETAPA_FLUXO_AMOSTRA" efe1_ on  this_."ID" = efe1_."ID_ENSAIO_AMOSTRA"
inner join "ETAPA" et2_ on efe1_."ID_ETAPA"=et2_."ID"
WHERE et2_."ID" in (5) and not (et2_."ID" = 6)
and this_."ID_ENSAIO" = 835
and efe1_."ID" = (
    select y0_ from
    ( SELECT this_0_."ID" as y0_
      FROM "ETAPA_FLUXO_AMOSTRA" this_0_
      WHERE this_0_."ID_ENSAIO_AMOSTRA" = this_."ID"
      ORDER BY this_0_."ID" desc )
)

如果我更改了这个数字的“ID”(已经存在的ensaio_amostra),它就可以了。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

x[t]

您不需要子查询,因为您已使用and efe1_."ID" = ( select y0_ from ( SELECT this_0_."ID" as y0_ FROM "ETAPA_FLUXO_AMOSTRA" this_0_ WHERE this_0_."ID_ENSAIO_AMOSTRA" = this_."ID" ORDER BY this_0_."ID" desc ) ) 子句加入了表:

INNER JOIN

只需使用:

FROM "ENSAIO_AMOSTRA" this_
inner join "ETAPA_FLUXO_AMOSTRA" efe1_ on  this_."ID" = efe1_."ID_ENSAIO_AMOSTRA"

在旁注中,子查询中的SELECT this_."ID" AS ID1_47_2_ FROM "ENSAIO_AMOSTRA" this_ INNER JOIN "ETAPA_FLUXO_AMOSTRA" efe1_ ON this_."ID" = efe1_."ID_ENSAIO_AMOSTRA" INNER JOIN "ETAPA" et2_ ON efe1_."ID_ETAPA" =et2_."ID" WHERE et2_."ID" IN (5) AND NOT (et2_."ID" = 6) AND this_."ID_ENSAIO" = 835; 毫无意义。此外,如果您在子查询中获得多行,则无论如何都会失败。