Domanda

From mysql ref manual: "Second or later SELECT statement in a UNION, dependent on outer query". But how UNION query can be dependent on outer query? Can someone give me clear example what does it mean? For instance, in this query:

SELECT * FROM sometable 
WHERE id IN (
    SELECT 1
    UNION
    SELECT 2 /* ! */
)

second select statement in union (which itself is subquery) has DEPENDENT UNION select_type and I dont know why, it must be just UNION because it doesn`t depend on anything.

È stato utile?

Soluzione

Well, the problem in this case is the IN operator, because subqueries in an IN clause are rewritten as correlated subqueries.

And a correlated subquery will give you a "dependent subquery" or a "dependent union" in case of an union.

See doc

The problem is that, for a statement that uses an IN subquery, the optimizer rewrites it as a correlated subquery. Consider the following statement that uses an uncorrelated subquery:

SELECT ... FROM t1 WHERE t1.a IN (SELECT b FROM t2); The optimizer rewrites the statement to a correlated subquery:

SELECT ... FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top