I found a weird disparity in mysql between using an ON and a WHERE to filter a subquery with a join.
This query runs fine:
SELECT * FROM cobrand co WHERE co.id IN (
SELECT co2.id FROM cobrand co3 INNER JOIN cobrand co2
ON co2.id = co3.id + 1
WHERE co2.id = co.id
)
But this one returns an error Unknown column 'co.id' in 'on clause'
:
SELECT * FROM cobrand co WHERE co.id IN (
SELECT co2.id FROM cobrand co3 INNER JOIN cobrand co2
ON co2.id = co3.id + 1
AND co2.id = co.id
)
Obviously the subquery's ON clause does not have access to to the outer query's alias, while the WHERE claus does. Why is this and can anyone point out where in the documentation this is covered?
EDIT: Removed unneeded background information involving pre-mature optimization.
-- MySQL docs, 13.2.9.2 JOIN Syntax
Please refer following Links
What's the difference between where clause and on clause when table left join?
In SQL / MySQL, what is the difference between "ON" and "WHERE" in a join statement?