With LEFT JOIN
SET @foo:=0;
SELECT @foo
FROM test t1
LEFT JOIN test t2 ON t2.id=t1.id AND (@foo:=@foo+1);
prints 0,0,0,0,0,0,0…
but with JOIN
SET @foo:=0;
SELECT @foo
FROM test t1
JOIN test t2 ON t2.id=t1.id AND (@foo:=@foo+1);
prints 1,2,3,4,5,6…
Anybody knows? Thanks!
UPD. This queries produce the same rows except @foo value
Assignments for session variables don’t work inside
ONclause.The reason you see @foo incremented for INNER JOIN is that optimizer runs your query as:
Using
EXPLAIN SELECT...you should seeusing wherein extra column.You can also use
EXPLAIN EXTENDED SELECT...and thenSHOW WARNINGSto see how exactly optimizer runs your query.In your case the
LEFT JOINquery is run exactly as it is written, as for this type of join you can get different results for the same condition inONandWHERE.If you want to increment @foo for
LEFT JOINuse: