Pregunta

i M tiene problema de sintaxis en mi consulta (simplificado):

select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
where not exists (select1 from table4 t4 where t4.pk1 = t1.pk1)

Al utilizar el "uso" palabra clave, doesnt oráculo permite identificador de tabla frente al nombre de la columna (por ejemplo: t1.pk1, solamente pk1 pueden ser utilizados)

Si escribo:

select *
from table1 t1
inner join table2 t2 using (pk1)
inner join table3 t3 using (pk2)
where not exists (select1 from table4 t4 where t4.pk1 = pk1)

Esta consulta no dará los resultados esperados.

Pero ya que estoy usando un "existe" subconsulta, ¿Cómo puedo formar parte de este sub consulta?

Por supuesto, supongo que podría escribir esta consulta otra manera y evitar el existe, o que no podía usar el "uso".

Sin embargo, es posible tener "unirse a / uso" combinado con una subconsulta en la cláusula where?

Editar: el uso de Oracle 10gR2

¿Fue útil?

Solución

Problema interesante! Lo mejor que puedo manejar sin dejar de utilizar utilizando es:

select * from
( select *
  from table1 t1
  inner join table2 t2 using (pk1)
  inner join table3 t3 using (pk2)
) v
where not exists (select1 from table4 t4 where t4.pk1 = v.pk1)

Otros consejos

No se puede utilizar la fase de clasificación de la tabla con juntas natural.

Esta consulta:

select 1 from table4 t4 where t4.pk1 = pk1

se analiza como

select 1 from table4 t4 where t4.pk1 = t4.pk1

y NOT EXISTS sobre ella siempre devuelve falso si no hay más que un solo registro en table4.

Sólo tiene que utilizar condiciones JOIN explícitas:

WITH    table1 AS
        (
        SELECT  1 AS pk1
        FROM    dual
        ),
        table2 AS
        (
        SELECT  1 AS pk1, 1 AS pk2
        FROM    dual
        ),
        table3 AS
        (
        SELECT  1 AS pk2
        FROM    dual
        ),
        table4 AS
        (
        SELECT  2 AS pk1
        FROM    dual
        )
SELECT  *
FROM    table1 t1
JOIN    table2 t2
ON      t2.pk1 = t1.pk1
JOIN    table3 t3
ON      t3.pk2 = t2.pk2
WHERE NOT EXISTS
        (
        SELECT  1
        FROM    table4 t4
        WHERE   t4.pk1 = t1.pk1
        )
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top