Bug #426 UNION results are messed up.
Submitted: 13 May 2003 15:01 Modified: 13 May 2003 23:43
Reporter: Arjen Schol Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:MySQL 4.0.12-standard-log OS:Linux (Linux 2.4.17.)
Assigned to: Assigned Account CPU Architecture:Any

[13 May 2003 15:01] Arjen Schol
Description:
Problems with (SELECT ..) UNION (SElECT ..).
Difficult to explain, but example should say enough.

How to repeat:
CREATE TABLE test (
  id int(11) NOT NULL default '0',
  value int(11) default NULL
) TYPE=MyISAM;

INSERT INTO test VALUES (1, 5);
INSERT INTO test VALUES (3, 6);
INSERT INTO test VALUES (2, NULL);
INSERT INTO test VALUES (5, NULL);

A simple query works as expected..

mysql> select * from test left join test as test2 on test.id=test2.value order by test.id;                                                              
+----+-------+------+-------+
| id | value | id   | value |
+----+-------+------+-------+
|  1 |     5 | NULL |  NULL |
|  2 |  NULL | NULL |  NULL |
|  3 |     6 | NULL |  NULL |
|  5 |  NULL |    1 |     5 |
+----+-------+------+-------+
4 rows in set (0.00 sec)

But this is weird..

mysql> (select * from test left join test as test2 on test.id=test2.value where test.id in (1,2)) union (select * from test left join test as test2 on test.id=test2.value where test.id in (3,5));                                 
+----+-------+----+-------+
| id | value | id | value |
+----+-------+----+-------+
|  1 |     5 |  0 |  NULL |
|  2 |  NULL |  0 |  NULL |
|  3 |     6 |  0 |  NULL |
|  5 |  NULL |  1 |     5 |
+----+-------+----+-------+
4 rows in set (0.00 sec)

This is even weirder! :-)

mysql> select test.id,test2.id,test.value,test2.value from test left join test as test2 on test.id=test2.value order by test.id;
+----+------+-------+-------+
| id | id   | value | value |
+----+------+-------+-------+
|  1 | NULL |     5 |  NULL |
|  2 | NULL |  NULL |  NULL |
|  3 | NULL |     6 |  NULL |
|  5 |    1 |  NULL |     5 |
+----+------+-------+-------+
4 rows in set (0.00 sec)

Looks okay, but this...

mysql> (select test.id,test2.id,test.value,test2.value from test left join test as test2 on test.id=test2.value where test.id in (1,2)) union (select test.id,test2.id,test.value,test2.value from test left join test as test2 on test.id=test2.value where test.id in (3,5));

But gives..

+----+----+-------+-------+
| id | id | value | value |
+----+----+-------+-------+
|  1 |  0 |  NULL |  NULL |
|  2 |  0 |  NULL |  NULL |
|  3 |  0 |  NULL |  NULL |
|  5 |  1 |  NULL |     5 |
+----+----+-------+-------+
4 rows in set (0.01 sec)
[13 May 2003 23:43] Michael Widenius
Thank you for your bug report. This issue has been fixed in the latest
development tree for that product. You can find more information about
accessing our development trees at 
    http://www.mysql.com/doc/en/Installing_source_tree.html

This bug is a duplicated of bug #386, which will be fixed in 4.0.13
(I did run your queries against the current 4.0.13 tree and they looked ok)