Bug #14817 Changed JOIN operator precedence
Submitted: 10 Nov 2005 10:04 Modified: 4 Oct 2008 10:01
Reporter: Matthias Pigulla Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server: DML Severity:S4 (Feature request)
Version:5.0.12 OS:Any
Assigned to: CPU Architecture:Any

[10 Nov 2005 10:04] Matthias Pigulla
Description:
In accordance with the manual, as of MySQL 5.0.12 the precedence of the JOIN and the comma operator have been changed.

This is a major showstopper when trying to upgrade to MySQL5, because you will have to scan all applications to see if they execute queries that are affected by this change and you will have to add parentheses to correct the problem. 

If you don't want to run different versions of MySQL in parallel, you're forced to touch "legacy" code that works with MySQL 4.1 or even earlier and that would not make use of the new features -- just to be able to use MySQL5 for "new" code.

See http://dev.mysql.com/doc/refman/5.0/en/join.html (the last two bullet points); the migration notes (http://dev.mysql.com/doc/refman/5.0/en/upgrading-from-4-1.html) also note this one as an "incompatible change", though I find the remark too subtle compared to the impact :).

How to repeat:
SELECT ... FROM a, b LEFT JOIN c ON c.c1 = a.a1 WHERE ...

This no longer works as because "b LEFT JOIN c" now has a higher precedence and thus in the ON clause you can only access tables b and c. Before the change, "," and "LEFT JOIN" had the same precedence so it would first join a and b and the result of that would be the operand for the LEFT JOIN.

The problem is that this way of writing a query is very "natural"... assume you have tables user, country and email, where each user comes from a country and have 0 ... n email addresses. Then you would "naturally" write the query to get all users, resolve the country and see if they additionally have an email address:

SELECT ... FROM user, country LEFT JOIN email ON email.userid = user.id WHERE user.countryid = country.id

Suggested fix:
Maybe add a switch or configuration option? Not nice, but I have no other good idea.
[10 Nov 2005 10:13] Valeriy Kravchuk
As you noted, it is the intended and documented behaviour now. 

As for the flag (or SQL mode) to change it back, I think, it is a feature request. Changed severity accordingly.
[4 Oct 2008 10:01] Konstantin Osipov
We have no plans to support the old, non-standard semantics.