Bug #37925 | Select with comma join pretends a column does not exist | ||
---|---|---|---|
Submitted: | 7 Jul 2008 12:21 | Modified: | 7 Jul 2008 14:10 |
Reporter: | Arjen Lentz | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Parser | Severity: | S2 (Serious) |
Version: | 5.0.51a | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | column, comma, join, parser |
[7 Jul 2008 12:21]
Arjen Lentz
[7 Jul 2008 12:43]
Arjen Lentz
Possibly a regression, the query works in 4.x (an existing production app was migrated from 4.x to 5.0, then triggering this error)
[7 Jul 2008 13:51]
Arjen Lentz
Being aware of - http://bugs.mysql.com/13551 - http://bugs.mysql.com/14817 and the information from the Docs (http://dev.mysql.com/doc/refman/5.0/en/join.html): 'However, the precedence of the comma operator is less than of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.' [...] 'Previously, the comma operator (,) and JOIN both had the same precedence, so the join expression t1, t2 JOIN t3 was interpreted as ((t1, t2) JOIN t3). Now JOIN has higher precedence, so the expression is interpreted as (t1, (t2 JOIN t3)). This change affects statements that use an ON clause, because that clause can refer only to columns in the operands of the join, and the change in precedence changes interpretation of what those operands are.' [end of quotes] That still leaves us with a unclear error reporting. Technically it may be correct based on parser internals, but users should not need to care about such black magic.
[7 Jul 2008 14:10]
MySQL Verification Team
Thank you for the bug report. From 5.0.12 the join syntax was changed according SQL:2003 standard. Please see: http://dev.mysql.com/doc/refman/5.0/en/join.html " Join Processing Changes in MySQL 5.0.12 Beginning with MySQL 5.0.12, natural joins and joins with USING, including outer join variants, are processed according to the SQL:2003 standard...." see below the change in FROM clause: mysql 5.0 > # now for the query that exhibits the problem: mysql 5.0 > mysql 5.0 > SELECT *, contact.phone AS personalPhone, contact.companyID AS contactCompanyID -> FROM user, contact -> LEFT JOIN company ON contact.companyID=company.id -> LEFT JOIN companysite ON company.id=companysite.companyID -> LEFT JOIN custom_user ON user.contactID=custom_user.contactID -> LEFT OUTER JOIN custom_state ON custom_state.stateName=companysite.state -> WHERE -> contact.id=user.contactID -> AND custom_user.isTeacher='1' -> AND user.active=1 -> AND custom_user.pageNameLink='blah' -> LIMIT 1; ERROR 1054 (42S22): Unknown column 'user.contactID' in 'on clause' mysql 5.0 > # now for the query that exhibits the problem: mysql 5.0 > mysql 5.0 > SELECT *, contact.phone AS personalPhone, contact.companyID AS contactCompanyID -> FROM (user, contact) -> LEFT JOIN company ON contact.companyID=company.id -> LEFT JOIN companysite ON company.id=companysite.companyID -> LEFT JOIN custom_user ON user.contactID=custom_user.contactID -> LEFT OUTER JOIN custom_state ON custom_state.stateName=companysite.state -> WHERE -> contact.id=user.contactID -> AND custom_user.isTeacher='1' -> AND user.active=1 -> AND custom_user.pageNameLink='blah' -> LIMIT 1; Empty set (0.03 sec)