Bug #2216 Bad subquery result
Submitted: 25 Dec 2003 19:58 Modified: 26 Dec 2003 7:49
Reporter: Igor Blagodetelev Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S1 (Critical)
Version:5.0 OS:Linux (Slackware)
Assigned to: Assigned Account CPU Architecture:Any

[25 Dec 2003 19:58] Igor Blagodetelev
Description:
If subquery 'where' expression use external query data on InnoDB tables, then result is wrong. This bug in MySQL 4.1.1 too.

How to repeat:
CREATE TABLE `test2` (
  `id` int(11) NOT NULL auto_increment,
  `val1` int(11) default NULL,
  `val2` char(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=koi8r;
insert into test2 values (1,10,'aaa');
insert into test2 values (2,50,'bbb');
insert into test2 values (3,999,'zzz');

mysql> select * from test2;
+----+------+------+
| id | val1 | val2 |
+----+------+------+
|  1 |   10 | aaa  |
|  2 |   50 | bbb  |
|  3 |  999 | zzz  |
+----+------+------+
3 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> select t1.id,(select val1 from test2 t2 where t1.id=t2.id) from test2 t1;
+----+-----------------------------------------------+
| id | (select val1 from test2 t2 where t1.id=t2.id) |
+----+-----------------------------------------------+
|  1 |                                            10 |
|  2 |                                          NULL |
|  3 |                                          NULL |
+----+-----------------------------------------------+
3 rows in set (0.01 sec)

If I use 'ENGINE=MyISAM', then result is:
+----+-----------------------------------------------+
| id | (select val1 from test1 t2 where t1.id=t2.id) |
+----+-----------------------------------------------+
|  1 |                                            10 |
|  2 |                                            50 |
|  3 |                                           999 |
+----+-----------------------------------------------+
3 rows in set (0.00 sec)
[26 Dec 2003 7:49] Dean Ellis
This would appear to be a duplicate of bug 2089.  You already have a similar test case in a comment on that issue.  After it has been corrected, the fix will be merged into the 5.0 tree.