Bug #1674 Incorrect results from nested join
Submitted: 27 Oct 2003 6:32 Modified: 27 Oct 2003 15:01
Reporter: Tony Ingraldi Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.0.14 OS:Linux (Linux)
Assigned to: CPU Architecture:Any

[27 Oct 2003 6:32] Tony Ingraldi
Description:
This issue has to do with the way in which MySQL interprets a nested simple join.  Suppose you have a database with three tables t1, t2, and t3 as shown below.

table t1:
+----+----------+
| id | t1name   |
+----+----------+
|  1 | t1_one   |
|  2 | t1_two   |
|  3 | t1_three |
+----+----------+

table t2:
+----+--------+
| id | t2name |
+----+--------+
|  1 | t2_one |
|  2 | t2_two |
+----+--------+

table t3:
+----+----------+
| id | t3name   |
+----+----------+
|  1 | t3_one   |
|  3 | t3_three |
+----+----------+

Now, you want to issue a query that will give you t1name, t2name, and t3name for all rows in t1 joined with t2 and t3 on the id column.  You want all of the rows in t1 to appear in the result no matter what is in t2 and t3.  You only want records from t2 and t3 to appear if both t2 and t3 have a match on the specified join condition.  An ANSI form of the desired query is:

SELECT t1.t1name, t2.t2name, t3.t3name
   FROM t1 LEFT OUTER JOIN (t2 JOIN t3 ON t2.id = t3.id)
   ON t1.id = t2.id;

In response to this query, the engine should first join t2 and t3 on the id column and then do an outer join of that result with t1.  The output should be:

+----------+--------+--------+
| t1name   | t2name | t3name |
+----------+--------+--------+
| t1_one   | t2_one | t3_one |
| t1_two   | NULL   | NULL   |
| t1_three | NULL   | NULL   |
+----------+--------+--------+

I have configured three commercial databases (Oracle 9i, Informix 9.2, FrontBase 3.6) with the sample tables shown above.  All three of these databases return the expected result when given the sample query.

While MySQL accepts the ANSI form of the query, the results obtained are not consistent with the results from the other three SQL engines tested.  Issuing the above query to MySQL will yield:

+----------+--------+--------+
| t1name   | t2name | t3name |
+----------+--------+--------+
| t1_one   | t2_one | t3_one |
| t1_two   | t2_one | NULL   |
| t1_three | t2_one | NULL   |
| t1_one   | t2_two | NULL   |
| t1_two   | t2_two | NULL   |
| t1_three | t2_two | NULL   |
+----------+--------+--------+

So, even though the query is accepted by the MySQL engine, the interpretation is quite different, and, I believe, incorrect.

How to repeat:
Configure sample database as described and issue query as described.
[27 Oct 2003 10:10] Dean Ellis
MySQL does not currently support nested joins and your parentheses are ignored.

This may have exposed another issue, which I will submit seperately myself, but nested joins are not supported at this time.

Thank you
[27 Oct 2003 15:01] Alexander Keremidarski
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments
to the original bug instead.

Thank you for your interest in MySQL.

Duplicate of #1591 http://bugs.mysql.com/1591